我的“帐户余额”变量未由代码更新

时间:2014-10-04 02:00:09

标签: javascript variables if-statement global-variables

这段代码基本上只是询问用户该做什么,然后,使用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; 

};

4 个答案:

答案 0 :(得分:0)

您必须在调用之前定义函数。如果你只是重新订购它们,你会没事的:http://jsfiddle.net/f4109zq9/

var withdrawl = function (dog){...}
var deposit = function(cat){ ...}

然后你的

do
{
   ...
}
while(){ .... }

答案 1 :(得分:0)

您的代码的问题在于您使用的是javascript函数,它们是在分析时而不是运行时定义的。

示例:

function deposit(amt) {
    //do stuff here
}

在运行时定义,可以放在代码中的任何位置。

BUT

var deposit = function(amt) {
    //do stuff here
}

是在解析时定义的。

这意味着你应该将它放在代码的顶部,否则,在调用时,变量函数“deposit”将是未定义的。

您的代码的工作示例如下。

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?"));


function deposit (cat) {
    actbal = +actbal + cat;
    return actbal; // DO NOT DOUBLE ADD




};

function withdrawl (dog) {
    actbal = actbal - dog;
    return actbal; // DO NOT DOUBLE SUBTRACT

};

答案 2 :(得分:0)

有两个问题。

首先,您在定义它们之前调用depositwithdrawl函数。如果您使用语法:

function deposit() {
    ...
}

您可以将定义放在任何位置,但是当您使用赋值时,它只会在脚本到达该行代码后生效。

其次,您在功能中添加和减少两次余额。您正在更新分配中的actbal功能,然后在return语句中再次添加/减去存款或提款金额。由于您已经更新actbal,因此您应该按原样返回:

var deposit = function (cat) {
    actbal = +actbal + cat;
    return actbal;
};

var withdrawl = function (dog) {
    actbal = actbal - dog;
    return actbal;
};

答案 3 :(得分:0)

使用function withdrawl()或在call之前写下add or subtract twice。对其他人也一样。并且不要在你的职能中{{1}}当前正在做的事情。