Tell us what’s happening:
Currently getting 3 of the test to pass, but like some other people finding a weird JS math error I can’t seem to correct. Any advice would be appreciated.
Your code so far
function checkCashRegister(price, cash, cid) {
let output = {status: '', change: []};
let change = cash - price;
let cidTotal = [];
for(let x in cid){
cidTotal.push(cid[x][1]);
}
cidTotal = cidTotal.reduce((a,b) => a + b).toFixed(2);
//making sure these values make sense
//console.log(cidTotal);
//console.log(change)
if(cidTotal < change){
output.status = "INSUFFICIENT_FUNDS";
return output;
} else {
if(cidTotal === change){
output.status = "CLOSED";
output.change = [...cid];
return output;
}
}
const denominations = [
{"name":"PENNY","value":0.01},
{"name":"NICKEL","value":0.05},
{"name":"DIME","value":0.10},
{"name":"QUARTER","value":0.25},
{"name":"ONE","value":1},
{"name":"FIVE","value":5},
{"name":"TEN","value":10},
{"name":"TWENTY","value":20},
{"name":"HUNDRED","value":100}]
let answer = [];
let tracker = 0;
for(let i = cid.length -1; i >= 0; i--){
while(change >= denominations[i].value && tracker < cid[i][1]){
change -= denominations[i].value
//here seems to be where the math error happens, but can't figure out
//how to fix it
// console.log(change)
tracker += denominations[i].value
} if(tracker > 0){
answer.push([denominations[i].name, tracker]);
} tracker = 0
}
// Here is your change, ma'am.
output.status = 'OPEN';
output.change = answer;
return output;
}
console.log(JSON.stringify(checkCashRegister(3.26, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]])));
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register