Tell us what’s happening:
I wrote this code for the Cash Register project. All of my results appear to be correct, but I keep failing the second and third tests. This is baffling me because I compared my results with the expected result like so…
let myResult = 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]])
console.log(myResult)
let fccResult = {status: "OPEN", change: [["QUARTER", 0.5]]};
console.log(fccResult)
if( JSON.stringify(myResult) === JSON.stringify(fccResult) ){console.log("they are the same")}
and it is still saying our results are different. I am stumped at this point. Any advice? Sorry for my code being such a mess.
Your code so far
//** CREATE A CURRENCY OBJECT */
function getCurrency(change, cid){
let num;
let str;
let index;
if(change >= 100 && cid[8][1] >= 100){
num = 100;
str = "ONE HUNDRED";
index = 8;
}
else if(change >= 20 && cid[7][1] >= 20){
num = 20;
str = "TWENTY";
index = 7;
}
else if (change >= 10 && cid[6][1] >= 10){
num = 10;
str = "TEN";
index = 6;
}
else if (change >= 5 && cid[5][1] >= 5){
num = 5;
str = "FIVE";
index = 5;
}
else if (change >= 1 && cid[4][1] >= 1){
num = 1;
str = "ONE";
index = 4;
}
else if (change >= 0.25 && cid[3][1] >= .25){
num = 0.25;
str = "QUARTER";
index = 3;
}
else if (change >= .1 && cid[2][1] >= .1){
num = 0.1;
str = "DIME";
index = 2;
}
else if (change >= .05 && cid[1][1] >= .05){
num = 0.05;
str = "NICKEL";
index = 1;
}
else if (change >= .01 && cid[0][1] >= .01){
num = 0.01;
str = "PENNY";
index = 0;
}
return {
num:num,
str:str,
index:index,
change:change
}
}
//** COUNT ALL FUNDS IN REGISTER */
function sumRegister(change, cid){
let sum = 0;
cid.forEach(function(element){
sum+=element[1];
});
return sum;
}
//** COUNT ALL FUNDS IN REGISTER THAT CAN BE USED FOR THE GIVEN CHANGE */
function sumUsableRegister(change, cid){
let sumOfUsableCurrency = 0;
let highestUsableCurrency = getCurrency(change,cid);
for(let i = highestUsableCurrency.index; i >= 0; i--){
sumOfUsableCurrency += cid[i][1];
}
return sumOfUsableCurrency;
}
let changeArr = []
//** GET CHANGE */
function generateChange(change, cid){
let currency = getCurrency(change,cid);
let remainder = 0;
let availableCash = cid[currency.index][1];
let toAdd;
if(Math.floor((currency.change / currency.num)) * currency.num > availableCash){
toAdd = (availableCash / currency.num) * currency.num;
}
else{
toAdd = Math.floor(change/currency.num) * currency.num;
}
remainder = currency.change - toAdd;
remainder = remainder.toFixed(2); //fix round issue\
console.log(`Add ${toAdd} in ${currency.str}s`);
console.log(`Remainder of ${remainder}`);
let subArr = [currency.str, toAdd];
changeArr.push(subArr);
if(remainder > 0){
generateChange(remainder,cid);
}
return;
}
//** MAIN */
function checkCashRegister(price, cash, cid) {
var change = cash - price;
let status;
let result;
//STATUS OF THE REGISTER
if(sumUsableRegister(change, cid) < change){
status = "INSUFFICIENT_FUNDS";
return{status:status, change:[]}
}
else if (sumRegister(change, cid) == change){
status = "CLOSED";
return{status:status, change:cid}
}
else{
status = "OPEN";
console.log("open status")
generateChange(change,cid);
let result = {status: status, change: changeArr};
console.log(result)
return result;
}
}
// 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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register