Tell us what’s happening:
Please find below code which is working fine in VS Code but keep on failing on FCC editor, I have no idea what’s going wrong.
Kindly review and let me know why FCC editor is failing
Your code so far
WARNING
The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.
You will need to take an additional step here so the code you wrote presents in an easy to read format.
Please copy/paste all the editor code showing in the challenge from where you just linked.
let returnObject = {
status: "",
change: []
}
function calculate(arrElement, baseCurrency, remainingAmount) {
let localArrayElement = [];
let cnt1 = arrElement[1] / baseCurrency;
for (let cnt2 = 0; cnt2 < cnt1; cnt2++) {
remainingAmount = remainingAmount - baseCurrency;
remainingAmount = Number(Math.round(remainingAmount + 'e2') + 'e-2');
console.log(remainingAmount + " " + baseCurrency);
if (remainingAmount < baseCurrency) {
localArrayElement[0] = arrElement[0];
localArrayElement[1] = baseCurrency * (cnt2 + 1);
returnObject.change.push(localArrayElement);
return remainingAmount;
}
}
localArrayElement[0] = arrElement[0];
localArrayElement[1] = baseCurrency * cnt1;
returnObject.change.push(localArrayElement);
return remainingAmount;
}
function checkCashRegister(price, cash, cid) {
if (cash > price) {
//let toBePaidBack = cash - price;
let moneyTBR = cash - price;
moneyTBR = Number(Math.round(moneyTBR + 'e2') + 'e-2');
let remainingAmount = moneyTBR;
//To set status OPEN, CLOSED, INSUFFICIENT_FUNDS
let totalAmount = 0;
for (let cnt = 0; cnt < cid.length; cnt++) {
totalAmount += cid[cnt][1];
}
totalAmount = Number(Math.round(totalAmount + 'e2') + 'e-2');
if (totalAmount < remainingAmount) {
returnObject.status = "INSUFFICIENT_FUNDS";
return returnObject;
}
for (let cnt = cid.length - 1; cnt >= 0; cnt--) {
switch (cnt) {
case 0:
if (remainingAmount >= 0.01 && cid[0][1] !== 0) {
totalAmount = totalAmount - remainingAmount;
remainingAmount = calculate(cid[0], 0.01, remainingAmount);
} else if (cid[0][1] === 0) {
returnObject.change.push(["PENNY", 0]);
}
break;
case 1:
if (remainingAmount >= 0.05 && cid[1][1] !== 0) {
totalAmount = totalAmount - remainingAmount;
remainingAmount = calculate(cid[1], 0.05, remainingAmount);
} else if (cid[1][1] === 0) {
returnObject.change.push(["NICKEL", 0]);
}
break;
case 2:
if (remainingAmount >= 0.1 && cid[2][1] !== 0) {
totalAmount = totalAmount - remainingAmount;
remainingAmount = calculate(cid[2], 0.1, remainingAmount);
} else if (cid[2][1] === 0) {
returnObject.change.push(["DIME", 0]);
}
break;
case 3:
if (remainingAmount >= 0.25 && cid[3][1] !== 0) {
totalAmount = totalAmount - remainingAmount;
remainingAmount = calculate(cid[3], 0.25, remainingAmount);
} else if (cid[3][1] === 0) {
returnObject.change.push(["QUARTER", 0]);
}
break;
case 4:
if (remainingAmount >= 1 && cid[4][1] !== 0) {
totalAmount = totalAmount - remainingAmount;
remainingAmount = calculate(cid[4], 1, remainingAmount);
} else if (cid[4][1] === 0) {
returnObject.change.push(["ONE", 0]);
}
break;
case 5:
if (remainingAmount >= 5 && cid[5][1] !== 0) {
totalAmount = totalAmount - remainingAmount;
remainingAmount = calculate(cid[5], 5, remainingAmount);
} else if (cid[5][1] === 0) {
returnObject.change.push(["FIVE", 0]);
}
break;
case 6:
if (remainingAmount >= 10 && cid[6][1] !== 0) {
totalAmount = totalAmount - remainingAmount;
remainingAmount = calculate(cid[6], 10, remainingAmount);
} else if (cid[6][1] === 0) {
returnObject.change.push(["TEN", 0]);
}
break;
case 7:
if (remainingAmount >= 20 && cid[7][1] !== 0) {
totalAmount = totalAmount - remainingAmount;
remainingAmount = calculate(cid[7], 20, remainingAmount);
} else if (cid[7][1] === 0) {
returnObject.change.push(["TWENTY", 0]);
}
break;
case 8:
if (remainingAmount >= 100 && cid[8][1] !== 0) {
totalAmount = totalAmount - remainingAmount;
remainingAmount = calculate(cid[8], 100, remainingAmount);
} else if (cid[8][1] === 0) {
returnObject.change.push(["ONE HUNDRED", 0]);
}
break;
}
}
if (totalAmount === 0) {
returnObject.status = "CLOSED";
}
else if (totalAmount > 0) {
returnObject.status = "OPEN";
}
} else {
console.log("insufficient cash");
}
return returnObject;
}
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_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.80 Safari/537.36
.
Challenge: Cash Register
Link to the challenge: