這段代碼基本上只是要求用戶該做什麼,然後使用2個函數來增加或減去我的實際變量的金額。但是,該變量未被代碼更新。我不明白爲什麼。我的「賬戶餘額」變量沒有被代碼更新
actbal = (100);
confirm("Would you like to view account balance? Place a deposit? Or make a withdrawl? Press OK to continue!");
do
{
var v = "VIEW";
var d = "DEP";
var w = "WITH";
var cont1 = prompt('Type: "VIEW" , "DEP" , or "WITH" to do their respective comands');
if(v === cont1.substring(0))
{
confirm("Your account balance is: " + actbal);
}
else if (d === cont1.substring(0))
{
var depo = prompt("How much do you want to deposit?");
var cat = parseFloat(depo);
if(cat < 0)
{
alert("You cant Deposit a negative number...");
}
else
{
confirm("Your new account balance is: " + deposit(cat));
//var actbal = actbal + cat;
}
}
else if (w === cont1.substring(0))
{
var With = prompt("How much do you want to withdrawl?");
var dog = parseFloat(With);
if(dog < 0)
{
alert("You cant Deposit a negative number...");
}
else
{
confirm("Your new account balance is: " + withdrawl(dog));
}
}
else
{
alert("You have not entered a command correctly!");
}
}
while
(
confirm("Do you want to continue with your account?")
);
var deposit = function(cat)
{
actbal = +actbal +cat;
return +actbal +cat;
};
var withdrawl = function (dog)
{
actbal = actbal - dog;
return actbal - dog;
};
爲什麼使用'cont1.substring(0)'而不是'cont1'? – Barmar 2014-10-04 02:12:49
當你正在調試時,你沒有注意到Javascript控制檯中的錯誤嗎? – Barmar 2014-10-04 02:23:37
嗯,刪除子串查找器後,它確實工作。我不期望它能夠輕鬆地工作。我會改變它以更好地簡化代碼,但是我沒有在代碼中發現任何錯誤。根據其他海報(並由他們證明),我的問題是我的函數聲明被錯誤地放置。無論如何感謝您的幫助! – ShowmenTv 2014-10-04 02:30:31