javascript - while loop not giving correct results -


i newbie js. trying code script when buy mobile price deducted credit card price. here code

    // total money in credit card var totalmoneyforcredit=150;  // current billing shop var moneyspent=0;  // prices of phones var samsungprice=33; var sonyprice=22; var nokiaprice=22;  // asseorices mobile  var charger=5; var headset=10;   // ask user purchasing mobies   while(totalmoneyforcredit>0){     var order=prompt("please enter mobile want purchase");     if (order==='sam') {         moneyspent=moneyspent+samsungprice;          totalmoneyforcredit=totalmoneyforcredit-moneyspent;      }     else if (order==='nokia') {         moneyspent=moneyspent+nokiaprice;         totalmoneyforcredit=totalmoneyforcredit-moneyspent;       }      else if (order==='sony') {         moneyspent=moneyspent+sonyprice;          totalmoneyforcredit=totalmoneyforcredit-moneyspent;      }     document.write(  '<b>' +  ' spent ' + moneyspent  + " , money left in credit " + totalmoneyforcredit + '</br>');  } 

it's working fine when shop nokia or phone 4th time prices displayed incorrectly.

snapshot

how come spent 88 out of 150 , still money left -70. please tell me doing wrong. thanks.

here how it, subtracting phone price total money instead of total money remaining amount.

    // total money in credit card  var totalmoneyforcredit=150;    // current billing shop  var moneyspent=0;    // prices of phones  var samsungprice=33;  var sonyprice=22;  var nokiaprice=22;    // asseorices mobile    var charger=5;  var headset=10;      // ask user purchasing mobies      while(totalmoneyforcredit>0){      var order=prompt("please enter mobile want purchase");      if (order==='sam') {          moneyspent=moneyspent+samsungprice;            totalmoneyforcredit=totalmoneyforcredit-samsungprice;        }      else if (order==='nokia') {          moneyspent=moneyspent+nokiaprice;          totalmoneyforcredit=totalmoneyforcredit-nokiaprice;          }        else if (order==='sony') {          moneyspent=moneyspent+sonyprice;            totalmoneyforcredit=totalmoneyforcredit-sonyprice;        }      console.log(' spent ' + moneyspent  + " , money left in credit " + totalmoneyforcredit);    }  alert('you ran out of money!');


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -