2013-12-14 107 views
-1

我在網上學習JavaScript造成的錯誤,我停留在一個點,我需要調用一個函數功能在javascript

var creditCheck = function (income) 
{ 
    if (income>=100) 
    { 
     return console.log("You earn a lot of money! You qulaify for a credit card."); 
    } 
    else { 
     return console.log("Alas yo do not qualify for credit card. Capitalism is creul like that."); 
    } 
}; 

creditCheck(75); 

以上是我的代碼在我打電話的creditCheck功能。現在當我把75的功能參數else命令(即"Alas yo do not qualify for credit card. Capitalism is creul like that.")在控制檯中執行兩次。

爲什麼此文本在控制檯中出現兩次?

感謝

+4

只出現在這裏一次。 –

+2

工作完美 - > http://jsfiddle.net/2JT37/ – adeneo

+0

爲什麼你要返回'console.log'? – tewathia

回答

3

編輯:CodeAcademy自動將調用與75參數的功能,所以沒有必要爲你調用該函數。對於你的代碼,你想返回一個字符串,所以不是返回console.log,返回字符串中的console.log裏面,像

return "You earn a lot of money! You qulaify for a credit card."; //or whatever the string is 

此代碼將通過測試:

var creditCheck = function (income) 
{ 
    if (income>=100) 
    { 
     return "You earn a lot of money! You qualify for a credit card." 
    } 
    else { 
     return 'Alas you do not qualify for a credit card. Capitalism is cruel like that.' ; 
    } 
}; 

當我在我的控制檯中運行它,我只得到一個聲明。我猜你在別處叫它。發佈您的整個代碼,否則將很難幫助您。但是,當您說return console.log()時,您每次都會返回undefined,因此它沒有任何用處。你可能想要做的是

var creditCheck = function (income) 
{ 
    if (income>=100) 
    { 
     console.log("You earn a lot of money! You qulaify for a credit card."); 
     return true; //or return "You earn a lot of money!" 
    } 
    else { 
     console.log("Alas yo do not qualify for credit card. Capitalism is creul like that."); 
     return false;//or return "You don't qualify"; 
    } 
}; 

creditCheck(75); 
+1

見毛刺http://meta.stackexchange.com/q/184939/210016 –

+0

@ scrblndr3:謝謝 這是整個代碼:D 我目前只測試這部分 – user2828488

+0

@ user2828488現在可以工作了,您將返回一個console.log(),它將評估每次返回undefined而不是返回一個結果 – scrblnrd3