Help for my code output comparison with FCC's

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:

The following is the code output comparisons:
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]])

should return an object.


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]])

should return {status: “OPEN”, change: [[“QUARTER”, 0.5]]}.
my code ouptut { status: ‘OPEN’, change: [ [ ‘QUARTER’, 0.5 ] ] }
______________________________________________________________________________________-
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]])

should return {status: “OPEN”, change: [[“TWENTY”, 60], [“TEN”, 20], [“FIVE”, 15], [“ONE”, 1], [“QUARTER”, 0.5], [“DIME”, 0.2], [“PENNY”, 0.04]]}.

my code output
{
status: ‘OPEN’,
change: [
[ ‘TWENTY’, 60 ],
[ ‘TEN’, 20 ],
[ ‘FIVE’, 15 ],
[ ‘ONE’, 1 ],
[ ‘QUARTER’, 0.5 ],
[ ‘DIME’, 0.2 ],
[ ‘PENNY’, 0.04 ]
]
}


checkCashRegister(19.5, 20, [[“PENNY”, 0.01], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 0], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]])

should return {status: “INSUFFICIENT_FUNDS”, change: }. officially passed! checked by FCC ( i omit the code output here)


checkCashRegister(19.5, 20, [[“PENNY”, 0.01], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 1], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]])

should return {status: “INSUFFICIENT_FUNDS”, change: }.
my code output { status: ‘INSUFFICIENT_FUNDS’, change: }


checkCashRegister(19.5, 20, [[“PENNY”, 0.5], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 0], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]])

should return {status: “CLOSED”, change: [[“PENNY”, 0.5], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 0], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]]}.

my code output:
{
status: ‘CLOSED’,
change: [
[ ‘PENNY’, 0.5 ],
[ ‘NICKEL’, 0 ],
[ ‘DIME’, 0 ],
[ ‘QUARTER’, 0 ],
[ ‘ONE’, 0 ],
[ ‘FIVE’, 0 ],
[ ‘TEN’, 0 ],
[ ‘TWENTY’, 0 ],
[ ‘ONE HUNDRED’, 0 ]
]
}
_________________________________________________________________________________________________________________-
Any help or hint will be greatly appreciated!!

If you add

console.log(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]]))

you can see that you are returning undefined. You are console.loging the object without returning it from your outer function.

1 Like

Thank you so much Jeremy!!! What a stupid glitch i made! I fixed it and it worked right away and passed the test. Truly appreciate your help!

1 Like

I’m glad I was able to help! Nice work getting the project to pass!

1 Like