Tell us what’s happening:
My tests run fine on the Chrome Console and Edge Console but the FCC test fails for this test case
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]]);
function checkCashRegister (price, cash, cid) {
console.log(cid);
//payout
let payout = +(cash - price).toFixed(2);
console.log("Payout: " + payout);
//Total Cash calculator
let totalCash = 0;
for (let x in cid){
totalCash = totalCash + cid[x][1];
}
totalCash = +totalCash.toFixed(2);
console.log("Total Cash: " + totalCash);
let balanced = {status: "CLOSED", change: cid};
let outOfMoney = {status: "INSUFFICIENT_FUNDS", change: []};
//funds == payout
if (totalCash == payout) {
return balanced;
}
//insufficient funds or change
else if (payout > totalCash){
console.log("Total Cash in poverty: " + totalCash);
console.log("Payout by poverty: " + payout);
return outOfMoney;
}
else {
let changeArr = [];
//enhance the cid
for (let x in cid) {
if (cid[x][0] == "PENNY" )
{cid[x].push(0.01);
}
else if (cid[x][0] == "NICKEL" )
{cid[x].push(0.05);
}
else if (cid[x][0] == "DIME" )
{cid[x].push(0.1);
}
else if (cid[x][0] == "QUARTER" )
{cid[x].push(0.25);
}
else if (cid[x][0] == "ONE" )
{cid[x].push(1);
}
else if (cid[x][0] == "FIVE" )
{cid[x].push(5);
}
else if (cid[x][0] == "TEN" )
{cid[x].push(10);
}
else if (cid[x][0] == "TWENTY" )
{cid[x].push(20);
}
else if (cid[x][0] == "ONE HUNDRED" )
{cid[x].push(100);
}
}
console.log("=============================");
console.log("Enhanced CID");
console.log(cid);
console.log("=============================");
//if there are enough total funds, check and calculate change
for (let i = cid.length -1 ; i >= 0 ; i-- ){
while (cid[i][1] > 0){
//for each bill type
if (cid[i][2] <= payout){
payout -= cid[i][2];
payout = +payout.toFixed(2); //still need to give
console.log(payout + " left. paying with " + cid[i][0]);
changeArr.push(cid[i][0]);
console.log(changeArr);
cid[i][1] -= cid[i][2];
//cid[i][1].toFixed(2);
if (payout == 0) {return {status: "OPEN", change: changeBrute(changeArr)}}
} //remaining billType balance
else {
console.log("BillType depleted" + cid[i][0] + ", payout left: " + payout + " Switching to next")
break;
}
}
} //end of for loop
console.log(changeArr);
console.log("Final Payout left " + payout);
if (payout > 0) {
console.log("outOfMoney by lack of change");
return outOfMoney}
else return {status: "OPEN", change: changeBrute(changeArr)}}
} //end of the function
var changeBrute = (arr) => {
let finalChange = [["ONE HUNDRED", 0],["TWENTY", 0], ["TEN", 0], ["FIVE", 0], ["ONE", 0], ["QUARTER", 0], ["DIME", 0], ["PENNY", 0]];
for (let x in arr) {
console.log(arr[x]);
if (arr[x] == "ONE HUNDRED"){
console.log("Adding 100");
finalChange[0][1] += 100;
}
else if (arr[x] == "TWENTY"){
console.log("Adding 20");
finalChange[1][1] += 20;
}
else if (arr[x] == "TEN"){
console.log("Adding 10");
finalChange[2][1] += 10;
}
else if (arr[x] == "FIVE"){
console.log("Adding 5");
finalChange[3][1] += 5;
}
else if (arr[x] == "ONE"){
console.log("Adding 1");
finalChange[4][1] += 1;
}
else if (arr[x] == "QUARTER"){
console.log("Adding 0.25");
finalChange[5][1] += 0.25;
}
else if (arr[x] == "DIME"){
console.log("Adding 0.05");
finalChange[6][1] += 0.05;
}
else if (arr[x] == "PENNY"){
console.log("Adding 0.01");
finalChange[7][1] += 0.01;
}
}
finalChange = finalChange.filter(x => x[1] != 0)
console.log(finalChange);
return finalChange}
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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register