I’ve been trying to run my code since last night and the Run tests button just doesn’t respond.
I always code on Repl and then paste it in the FCC editor. I don’t know what the problem is.
My Code:
function checkCashRegister(price, cash, cid) {
// Here is your change, ma'am.
let changeToReturn = cash - price;
let changeArray = [["PENNY", 0],["NICKEL", 0],["DIME", 0],
["QUARTER", 0],["ONE", 0],["FIVE", 0],["TEN", 00],["TWENTY", 0],["ONE HUNDRED", 0]];
let copyCid = cid.slice();
let cashAvailable = 0;
for(let i=0; i<copyCid.length; i++){
cashAvailable += copyCid[i][1];
}
let sumChange = 0, changeAvailable;
let j = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
//console.log(changeToReturn);
for(let i=copyCid.length-1; i>=0; i--){
sumChange = j[i];
//console.log("-----"+sumChange);
while(changeToReturn/j[i]>=1 && copyCid[i][1]>=j[i]){
changeToReturn -= j[i];
changeToReturn = (changeToReturn).toFixed(2);
copyCid[i][1] -= j[i];
copyCid[i][1] = copyCid[i][1].toFixed(2);
changeArray[i][1] += j[i];
//changeArray[i][1] = parseFloat(changeArray[i][1]).toFixed(2);
cashAvailable -= j[i];
cashAvailable = cashAvailable.toFixed(2);
// console.log(cashAvailable);
// console.log(" = "+sumChange+"==="+j[i]+"==="+copyCid[i][1]+"=="+changeArray[i][1]);
sumChange += j[i];
}
//console.log("*"+changeToReturn);
if(changeToReturn<=0.0){
changeAvailable = true;
//console.log("Change successfully returned");
// break;
}
}
//arranging array from larger bills to smaller, 100 - 0.01
let finalArray = [];
for(let i=changeArray.length-1; i>=0; i--){
finalArray.push(changeArray[i]);
}
if(changeAvailable){
if(cashAvailable <= 0.01){
return {
status: "CLOSED",
change: finalArray
};
}
else{
return{
status: "OPEN",
change: finalArray.filter(val => val[1]>0)
};
}
}
else{
return{
status: "INSUFFICIENT_FUNDS",
change: []
};
}
return cashAvailable;
}
My code is not perfect yet, some minor changes are needed, but at least the test should show tests failed error, it just doesn’t seem to bother with the click. I’ve checked for infinite loops, the code works smoothly. You can doublecheck it here https://repl.it/@VineshSwargam/CashRegister
One other problem apart from Run Tests button is that, in the code link I gave, if you see there, I used the slice method to copy the cid array into another array to perform operations on the copied array so as to not mutate the original array. But, after the code runs past a for loop( one with the while loop in it), the cid array, which is not even present in any calculations, is mutated in the last iteration of the for loop. I’ve been trying to understand what’s going on, but nothing seems wrong to me.
Any help is much appreciated.
Thank you!