Cash register, help needed, I'm losing my mind

Tell us what’s happening:

The whole course of JS went quite good, but this one is making me lose my mind. I’ve spent days on this task and i just cannot figure out why the hell it isn’t working…

It just does not return change. Help!!!

   **Your code so far**

function checkCashRegister(price, cash, cid) {
var chang = cash - price;
var split = [].concat.apply([], cid);
var cashVal = [0.01,0.05,0.10,0.25,1,5,10,20,100];  

// creating an array of numeric values of cid
var filtered = split.filter(function(item) {
 return (typeof item === "number")

//total cash in the register
var sum = filtered.reduce(function(a, b) { return a + b; }, 0);
sum = Math.round(sum*100) / 100;

//insufficient funds
 if (sum < chang){
   return {status: "INSUFFICIENT_FUNDS", change: []};

//cash in register is equal to required change
if (sum === chang){
   var retuns =
   {status: "CLOSED", 
   change: cid};
   return retuns;

//this is where the trouble begins :((((
 // when we have enough cash in register to return change
if (sum > chang) {

for (var i = (filtered.length - 1); i > -1; i--){
   var array = [];
   var value = 0;

//repeat while needed change is larger than the value of bills, and when we have the forementined bills in the cash drawer
   while (chang >= cashVal[i] && filtered[i]>0) {

     //if the total value of the certain bill is equal or smaller than the change, we take the whole amount and declare it the new value
   if (filtered[i] <= chang) {chang -= filtered[i];
     value += filtered[i];} 
     //if change is smaller than the total value of bills, we dont need the total amount, thus we will only the part that we need according to the value of a bill. 
   else { value += chang - (chang % cashVal[i])}
   return array = array.push(cid[i][0], value );
//always shows an empty array??? howwwww????? whyyyy????

 var stats = {status: "OPEN", change: [array]};
 stats.change = array;
 return stats;

//i cant figure out how to write this part.. help
else {
 for (var i = (cid.length - 1); i > -1; i--) {
   if (cid[i][1]>0){
     var boom = {status: "INSUFFICIENT_FUNDS", change: [array]};
     boom.change = cid[i];
     return boom;
}This text will be hidden
console.log(checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]));

   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36.

Challenge: Cash Register

Link to the challenge:

So I managed to figure out the part when you cannot return the exact change to insufficient funds.
yet I still don’t understand why where it has to display change it just does not… what went wrong?

if (value > 0){
  var stats = {status: "OPEN", change: [array]};
  stats.change = array;
  return stats;} 
  else {
    return {status: "INSUFFICIENT_FUNDS", change:[]}};```

yet I still don't understand why where it has to display change it just does not... what went wrong?

Notice array is declared within for loop, this potentially makes new array for each iteration, disregarding past entries. That might be one reason why array always appear empty, another one is line:

return array = array.push(cid[i][0], value );

Take a look at what is happening here, first two entries are pushed to array. push method of array returns new length of array, assigning that to array. This would make array variable contain something, what could be shown later in stats. Notice however that there’s also return returning from function.

Some confusion might be occurring due to inconsistent code style formatting. In some parts it takes a while to understand which closing bracket is corresponding to which opening one, and therefore what is part of which loop and what not. Try to clean that up, it will be easier to read code and follow it.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.