Tell us what’s happening:
I checked all the required tests, it passed, which means the output is exactly same as the required results. I use d both Chrome console and Atom to check. Both gives me the same results as passing the tests require. However, when I submit my code, it only shows one test passes, all others failed, even though I compared my test result with the result provided by FCC to find no differences.
Would someone kindly helps me take a look at the reason of failure? It really throws me off even though i checked numerous times.
Your code so far
function checkCashRegister(price, cash, cid) {
var denom = [0.01, 0.05, 0.10, 0.25, 1.00, 5.00, 10.00, 20.00, 100.00];
var change = Number((cash - price).toFixed(2));
var value = 0;
var holder = [];
var output = {status: null, change: null}
var totalInDrawer = cid.map(e=>e[1]).reduce((a,b)=>a+b)
if(totalInDrawer<change) {output.status ="INSUFFICIENT_FUNDS"; output.change = []; console.log(output);return output;}
if(totalInDrawer>change) {output.status ="OPEN"; cashSorter()}
if(totalInDrawer===change){output.status ="CLOSED"; cashSorter() }
function cashSorter() {
for(let i = denom.length-1; i>-1; i--) {
while(change>=denom[i] && cid[i][1]>0) {
change = Number(change.toFixed(2))
change -= denom[i]; value += denom[i]
cid[i][1]-= denom[i]
}
if(value!=0){holder.push(cid[i][0], Number(value.toFixed(2))); value=0;} //don't show elements with its zero value in the holder array as well as the final output.change array
if(cid[i][1]===0 && !holder.includes(cid[i][0])){holder.push(cid[i][0], 0);}
}
output.change = chunkArrayInGroups(holder); // take the holder array to chunk its elements into groups as the required output format
var outputSum = output.change.map(e=>e[1]).reduce((a,b)=>a+b)
if(change>outputSum){output.status = "INSUFFICIENT_FUNDS"; output.change = []; console.log(output);return output;}
if(output.change[0][1]===0){output.change.reverse()}// without this code, still works! But this is really for the purpose of passing the required test format although it is not necessary to reverse. i'd rather not having it if not for the sake of passing the test.
console.log(output);
return output
}
function chunkArrayInGroups(arr) {
let newArr = [];
let j = 0;
while (j < arr.length) { newArr.push(arr.slice(j, j + 2)); j+=2 }
return newArr;
}
}
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36
.
Challenge: Cash Register
Link to the challenge: