gpt4 book ai didi

javascript - 加等于运算符(operator)错误

转载 作者:行者123 更新时间:2023-11-28 01:33:31 24 4
gpt4 key购买 nike

您好,我正在尝试使用 += 来增加余额值。我现在明白在java脚本中使用+=是按引用传递,但是我如何使用它按值传递。

alert("Welcome to the Online Bank Teller");

var balance = 100.00;
var amount;
var run = true;

do{

var pick = prompt("Make a selection...\n1-Check Balance, 2-Deposit, 3-Withdraw, 4-Quit");

if(pick == 1){alert("Your balance is: $" + balance.toFixed(2));}
else if(pick == 2){
amount = prompt("Enter the amount you want to deposit: $");

if(amount > 1000){alert("You can only enter up to $1000 per deposit!");}
Right here--->balance += amount;
alert("Your new balance: $" + balance.toFixed(2));
}
else if(pick == 3){
amount = prompt("Enter the amount you want to withdraw: $");

if(amount > balance){alert("Amount exceeded account balance!");}
else if(amount > 500){alert("The max you can take out is up to $500 per withdraw!");}
else if (amount <= balance){
balance -= amount;
alert("Your new balance: $" + balance.toFixed(2));
}
}
else if(pick == 4){run = false;}
else{alert("Not a valid choice!");}

}while(run)

当用户输入新的存款时,如何让它改变变量内部的值。

我明白了

Your balance is: $10022

而不是

Your balance is: $122

提前致谢...

最佳答案

对从提示中获取的每个金额使用parseInt()函数

amount = parseInt(prompt("Enter the amount you want to deposit: $"), 10);

DEMO

关于javascript - 加等于运算符(operator)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21801670/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com