2014-10-04 90 views
0

這段代碼基本上只是要求用戶該做什麼,然後使用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; 

}; 
+0

爲什麼使用'cont1.substring(0)'而不是'cont1'? – Barmar 2014-10-04 02:12:49

+0

當你正在調試時,你沒有注意到Javascript控制檯中的錯誤嗎? – Barmar 2014-10-04 02:23:37

+0

嗯,刪除子串查找器後,它確實工作。我不期望它能夠輕鬆地工作。我會改變它以更好地簡化代碼,但是我沒有在代碼中發現任何錯誤。根據其他海報(並由他們證明),我的問題是我的函數聲明被錯誤地放置。無論如何感謝您的幫助! – ShowmenTv 2014-10-04 02:30:31

回答

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 

}; 
+0

感謝您的幫助和良好解釋的修復。您還修復了我的功能中的愚蠢錯誤,但另一位用戶看到了它並告訴了我這個問題。但是有一個問題,如果我在代碼中將它們稱爲更高的代碼,代碼如何知道函數是在底部定義的?爲什麼更改變量的使用可以讓代碼沿着其「軌跡」進一步查看,比如果我說var x = function()...? – ShowmenTv 2014-10-04 02:36:11

+0

感謝您的回覆!這是javascript設計的方式。如果你定義了一個變量,任何變量,比方說'var a ='dog';'你只能訪問這個變量** AFTER **它被定義了。變量函數也一樣。但是,以'function name_of_function()'開頭的函數在運行時被解析,這意味着** BEFORE **程序啓動時,解析器讀取所有函數並將其放入內存中。這就是爲什麼你可以在你的代碼中的任何地方訪問它。 :) – 2014-10-04 02:44:30

0

你必須在你調用它們之前定義你的函數。如果你只是重新排列它們,你會被罰款:http://jsfiddle.net/f4109zq9/

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

,然後你

do 
{ 
    ... 
} 
while(){ .... } 
+0

您的修補程序是正確的,但您在該鏈接內放置的代碼無法正常工作。哈哈我有我的代碼無論如何工作,所以謝謝你的幫助! – ShowmenTv 2014-10-04 02:37:27

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; 
}; 
+0

現在,我檢查我不知道爲什麼那裏。我一定嘗試了一些愚蠢的解決方法,並保持它不改變它,很好的捕獲。每個人都告訴我我的功能被濫用/錯位。謝謝! – ShowmenTv 2014-10-04 02:33:03

0

要麼使用function withdrawl()或他們你call之前寫的。其他人也一樣。並且不要在你的函數中使用add or subtract twice