2016-03-07 18 views
4

我還是一個初學者,在這裏總共noob,所以請耐心等待,我來到這個練習基本上是分散到25,10,5,2,1足夠數量的硬幣/美分的金額。如何在JavaScript中僅使用條件語句來編寫硬幣計數器函數?

因爲我還在學習,所以不熟悉內置函數,我試圖只寫條件語句。

功能與所測試的數目65效果很好,得到的25的陣列,25%,10%,5分別

然而,當我用數字46測試時,它給了25的陣列,25個,10 ,5,25,10,10,1這顯然是錯誤的,而且我的功能顯然是錯誤的。

請您將錯誤指向我。

這裏是我的代碼,並提前感謝。

var co = []; 
function coin(n){ //46 


// co = [25,] 


    if (n>=25){ 
     co.push("25"); 
     n = n-25; 
     coin(n); 
    } 

    else if (n>=10){ 
     co.push("10"); 
     n = n-10; 
     coin(n); 
    } 


    else if (n>=5){ 
     co.push("5"); 
     n = n -5; 
     coin(n); 
    } 

    else if (n>=2){ 
     co.push("2"); 
     n = n - 2; 
     coin(n); 
    } 

    else if (n>=1){ 
     co.push("1"); 
     n = n - 1; 
     coin(n); 
    } 




    else if (n == 0){console.log(co);} 


} 

回答

3

問題是當您第二次調用該函數時,您不清除該數組。您需要每次清除它。

你可以做一個新的功能Countcoin(n)

function Countcoin(n){ 
    co = []; 
    coin(n) 
} 

現在調用這個函數,而不是coin


或者你也可以建立只有一個功能,但使用循環,沒有遞歸。

var co = []; 
function coin(n){ 
    co=[]; 
    while(n!=0){ 
     if (n>=25){ 
      co.push("25"); 
      n = n-25; 
     } 
     else if (n>=10){ 
      co.push("10"); 
      n = n-10; 
     } 
     else if (n>=5){ 
      co.push("5"); 
      n = n -5; 
     } 
     else if (n>=2){ 
      co.push("2"); 
      n = n - 2; 
     } 
     else if (n>=1){ 
      co.push("1"); 
      n = n - 1; 
     } 
    } 
    console.log(co); 
} 
+0

你沒有遞歸的第二種解決方案是輝煌的。謝謝 請問爲什麼你需要重新分配co作爲函數中的空數組? –

+1

因爲它是一個全局變量,這意味着每次調用函數時它都不會被清除。相反,它會將新的解決方案添加到舊的解決方案中。如果我沒有重新分配它,那麼發生在你身上的問題將再次發生;從先前調用該函數存儲的數字將保留在「co」數組中。 –

+1

如果你不需要'co'成爲一個全局變量,你可以在第二個函數中聲明它,而不用重新分配它。 –

1

你的代碼工作得很好。但你只有全球陣列co它保留了以前的答案。所以我認爲你應該在函數中初始化一個數組,然後返回它。

1

你可以做每一個條件是:

  1. 簡單的除法(向下取整)

  2. 分配商到VAR或元素(比如輸出標籤)

  3. 使用該模爲下一個條件

片段

<!doctype html> 
 
<html> 
 
<head> 
 
<meta charset="utf-8"> 
 
<title></title> 
 
<style> 
 
fieldset { width: 20ex; font: 400 16px/1.4 'consolas'; color: #00e; } 
 
legend { font:inherit; font-size: 1.25rem; } 
 
input, label, output { padding: 1px 3px; font:inherit;} 
 
input { width: 7ex; } 
 
output { width: 5ex; } 
 
</style> 
 
</head> 
 

 
<body> 
 
<header></header> 
 
<form id="change" name="change"> 
 
<fieldset> 
 
<legend>coinCounter</legend> 
 
<label for="inp1">$</label><input id="inp1" name="inp1" placeholder="0.00"><br/> 
 
<label for="q">Quarters: </label><output id="q" name="q" for="inp1">0</output><br/> 
 
<label for="d">Dimes:&nbsp;&nbsp;&nbsp; </label><output id="d" name="d" for="inp1">0</output><br/> 
 
<label for="n">Nickels:&nbsp; </label><output id="n" name="n" for="inp1">0</output><br/> 
 
<label for="p">Pennies:&nbsp; </label><output id="p" name="p" for="inp1">0</output> 
 
</fieldset> 
 
</form> 
 
<footer>Hit <kbd>&#9166; Return</kbd> before typing</footer> 
 

 
<script> 
 
var inp1 = document.getElementById('inp1'); 
 

 
inp1.addEventListener('input', function(e) { 
 
\t delay(coinCounter(this.value), 5000); 
 
}, false); 
 

 
function coinCounter($) { 
 
\t var denomination = $.split('.'); 
 
\t var dollars = Number(denomination[0] * 100); 
 
\t console.log('dollars: '+dollars); 
 
\t var cents = Number(denomination[1]); 
 
\t console.log('cents: '+cents); 
 
    var total = Number(dollars + cents); 
 
    console.log('total: '+total); 
 
\t var modQ, modD, modN; 
 
\t 
 
\t if(total >= 25){ 
 
\t \t var quarter = Math.floor(total/25); 
 
\t \t console.log('quarters: '+quarter); 
 
\t \t document.getElementById('q').innerHTML = quarter; 
 
\t } 
 
\t modQ = total % 25; 
 
\t 
 
\t if(modQ >= 10){ 
 
\t \t var dime = Math.floor(modQ/10); 
 
\t \t console.log('dimes: '+dime); 
 
\t \t document.getElementById('d').innerHTML = dime; 
 
\t } 
 
\t modD = modQ % 10; 
 
\t 
 
\t if(modD >= 5){ 
 
\t \t var nickel = Math.floor(modD/5); 
 
\t \t console.log('nickels: '+nickel); 
 
\t \t document.getElementById('n').innerHTML = nickel; 
 
\t } 
 
\t modN = modD % 5; 
 
\t 
 
\t if(modN){ 
 
\t \t var penny = Math.floor(modN); 
 
\t \t console.log('pennies: '+penny); 
 
\t \t document.getElementById('p').innerHTML = penny; 
 
\t } 
 
\t else { 
 
\t \t modN = 0; 
 
\t } 
 
    return false; 
 
} 
 

 
var delay = (function(){ 
 
    var timer = 0; 
 
    return function(callback, ms){ 
 
    clearTimeout (timer); 
 
    timer = setTimeout(callback, ms); 
 
}; 
 
})(); 
 
</script> 
 
</body> 
 
</html>

+0

非常感謝zer00ne –

+1

歡迎您,先生。 – zer00ne

1

既然你提到你在學習,我想我會提出另一種方法 - 我提供一步步崩潰。

內置函數中使用:

Array.prototype.reduce - 針對適用的儲液器的功能和所述陣列的每一值(從左到右),以將其降低到一個值。

function coins(cents) { 
    return [25, 10, 5, 1].reduce(function(totalDenoms, denom) { 

    // if we have any values for this denomination, add it to the final array 
    for (var i = 0; i < Math.floor(cents/denom); i++) { 
     totalDenoms.push(denom); 
    } 

    // set the remaining amount of cents to process 
    cents = cents % denom; 

    // return the accumulated totals 
    return totalDenoms; 

    }, []); 
} 
+0

感謝您的幫助bruchowski :) –

+1

沒問題,祝你好運 – bruchowski