Tell us what’s happening:
Hello and thanks in advance for any help. My code passes all the tests in my environment. I am using Brackets and Chrome latest version 71. In the freeCodeCamp site it fails the last to tests. I tried to submit in FCC using both Chrome and Safari, but no go. It still fails the last to tests.
Your code so far
function checkCashRegister(price, cash, cid) {
var mon = {'ONE HUNDRED':100.0, 'TWENTY':20.0, 'TEN':10, 'FIVE':5, 'ONE':1, 'QUARTER':0.25, 'DIME':0.1, 'NICKEL':0.05, 'PENNY':0.01};
var notes = Object.keys(mon);
cid.reverse();
var whatINeed = [];
let count = 0, j = 0;
var change = {
status: "INSUFFICIENT_FUNDS",
change: []
};
let result = cash - price;
let cidValue = cid.reduce((acc, val)=> acc+ val[1],0);
const findChange = num => {
j = 0;
for (let i in mon){
count = 0;
while (num >= mon[i]){
count+=1;
num = Math.round((num - mon[i])*100)/100;
if (count * mon[i] === cid[j][1]){ //break go to next note
break;
}
}
whatINeed.push([mon[i],count]);
j++;
}
}
if (result > cidValue) {
return change;
} else if (result === cidValue){ // do the deed
findChange(result);
change.status = "CLOSED";
} else if (result < cidValue){
findChange(result);
change.status = "OPEN";
}
for (let i = 0; i < whatINeed.length; i++){
if (whatINeed[i][1] != 0) {
change.change.push([notes[i], whatINeed[i][0]*whatINeed[i][1]]);
}
}
// Here is your change, ma'am.
return (change);
}
// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.1],
// ["QUARTER", 4.25],
// ["ONE", 90],
// ["FIVE", 55],
// ["TEN", 20],
// ["TWENTY", 60],
// ["ONE HUNDRED", 100]]
checkCashRegister(19.5, 20, [["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_11_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.1.2 Safari/605.1.15
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register/