How to write a coin counter function using only conditional statements in JavaScript?

I'm still a beginner, here is a complete noob, so please be patient, I went through this exercise, which basically extends the amount of penny / cent to a sufficient number of coins 25,10,5,2,1.

I tried to write only with conditional expressions, since I am still involved, therefore, I am not familiar with the built-in functions.

the function works well with checked number 65, giving an array of 25, 25, 10, 5 respectively

however, when I tested with number 46, it gave an array of 25, 25, 10, 5, 10, 10, 1, which is obviously wrong, and something clearly doesn't match my function.

Could you point out the error.

here is my code and thanks in advance.

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);}


}
+4
source share
4

, , . .

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);
}
+3

. co, . , , .

+1

:

  • ( )

  • quotient var element (, )

  • modulo

<!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) {
	delay(coinCounter(this.value), 5000 );
}, false);

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

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

, , , - .

:

Array.prototype.reduce - applies a function against the accumulator and each value of the array (from left to right) to reduce this to a single value.

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;

    }, []);
}
+1
source

All Articles