1
\$\begingroup\$

Pooja would like to withdraw X USD from an ATM. The cash machine will only accept the transaction if X is a multiple of 5, and Pooja's account balance has enough cash to perform the withdrawal transaction (including bank charges). For each successful withdrawal, the bank charges 0.50 USD. Calculate Pooja's account balance after an attempted transaction.

Please review my code.

   var x= parseInt(prompt("put the value of account balance"));
  var y=parseInt(prompt("put the value of widrawal cash "));
     const charges=0.50;

      if(y%5==0 && y<x)
    {
alert((x-y)-charges)
            }else
   {
           alert(x)
      }
\$\endgroup\$

2 Answers 2

4
\$\begingroup\$

From a short review;

  • If you are not going to change the value of a variable, use const, not var
  • Avoid spelling mistakes; widrawal -> withdrawal
  • The if statement does not take the charges in to account
  • The indentation of this code is bad, please consider using a js formatter
  • alert and prompt are considered only okay for the most basic exercises
  • x and y are very poor names, how about accountBalance and requestedAmount?

This is my counter proposal

function performWithdrawal(){
  const accountBalance = parseInt(prompt("What is the value of the account balance?"));
  const requestedAmount = parseInt(prompt("How much do you wish to withdraw?"));
  const withdrawalCharges = 0.5;

  if(requestedAmount % 5 == 0 && (requestedAmount + withdrawalCharges <= accountBalance)){
    alert(`Your new bank account balance is ${accountBalance - requestedAmount - withdrawalCharges}`)
  } else {
    alert(`Your bank account balance is too low to withdraw ${requestedAmount} USD`);
  }    
}
\$\endgroup\$
1
  • \$\begingroup\$ Thank you for your valuable feedback and comment Sir \$\endgroup\$ May 16 at 13:09
0
\$\begingroup\$

There is no reason that the amounts have to be integers. By using parseInt(), you end up truncating the decimal portion of the input, which is wrong.

\$\endgroup\$
4
  • \$\begingroup\$ I am not sure how any non-integer amount can be a multiple of 5? Hmm, your point stands for the bank balance. \$\endgroup\$
    – konijn
    May 18 at 6:37
  • \$\begingroup\$ @konijn That is a problem, though. If a user asks to withdraw $5.99, it thinks you want to withdraw $5 and allows it. \$\endgroup\$ May 18 at 7:08
  • \$\begingroup\$ We should move to chat, but I would rather get 5 dollars instead of a message that it has to be a multiple of 5, then re-enter 5, and then finally get 5 dollars ;) \$\endgroup\$
    – konijn
    May 18 at 15:36
  • \$\begingroup\$ @konijn But what if you really needed $5.99? Now you have $5 and are $.99 short; if you withdraw another $5, it costs you $.50 more. But if you receive "needs to be a multiple of 5", you can enter $10 and have $4.01 extra. Alternately, a better UI might be to reply, "Needs to be a multiple of $5; do you want to withdraw $10? (Y/N)" which would streamline that interaction while still being accurate. Or offer more choices: $5 (rounded down); $10 (rounded up); something else. \$\endgroup\$
    – mdfst13
    May 23 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.