Cash Register Project Help

Hi, i’m not sure why the object i’m returning is not passing the tests. it seems to be returning an object with a status key and a change key that has an array with subarrays containing the denomination and amount in highest to lowest order. i’m not sure if it’s something to do with objectchange.change = xchange. is there a way to push the subarrays directly into the change key as the code iterates through each denomination?

I’ve been working on this code for awhile and just cant seem to see what’s wrong, any help is much appreciated!

var changearray = [];
var xchange=[];
    var cidnumberarray = [];
    var cidwordarray= [];



function checkCashRegister(price, cash, cid) {
    
      var objectchange = {};

    var change = cash - price;
    
    for (var i = 0; i < cid.length; i++) {
        for (var x = 0; x < cid.length; x++) {
            
            if (typeof cid[i][x] == "number") {
                cidnumberarray.push(cid[i][x]);



            }
            else {cidwordarray.push(cid[i][x]);}

        }
    }



    var cidtotal = cidnumberarray.reduce(function(acc, element) {
        return element + acc;
    })
    


        if (cidtotal < change) {
            objectchange["status"];
          objectchange["change"];
 objectchange.status = "INSUFFICIENT_FUNDS";
 objectchange.change = [];

        } else if (cidtotal === cash) {
            objectchange["status"];
          objectchange["change"];
          objectchange.status = "CLOSED";
           objectchange.change = [cidtotal];
        }

 

var coins = [
    [100],
    [20],
    [10],
    [5],
    [1],
    [0.25],
    [0.10],
    [.05],
    [.01]
];
 var filteredcidwordarray = cidwordarray.filter(function(element){
              return element != null;
            })
            var reversedcidwordarray = filteredcidwordarray.reverse();
var numberarray2 =cidnumberarray.reverse();

for (var i = 0; i < coins.length; i++) {

    var num = ((change * 100) / (coins[i] * 100));
    num  = num.toFixed(2);
 
  
    
    var roundednum = Math.floor(num);
     console.log("roundednum " +roundednum )

    var amount = roundednum * coins[i];



   if (amount > numberarray2[i]){
    amount = amount - coins[i];
   }

    var amount2 = numberarray2[i] - (amount/100);



    if (roundednum >0  && amount2 >0) {

            change = change - amount;
         
          
          objectchange["status"];
          objectchange["change"];

           objectchange.status = "OPEN";
             objectchange.change = [];
             xchange.push([reversedcidwordarray[i], amount]);
                     
             
            
          
        }




    }
     objectchange.change = xchange;

    return objectchange
 
}

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

1 Like

I just went through your code real quick.
What I see is that object.change is an array that can contain sub-arrays. Probably you want to try using object.change.push(subarray) and check your code.

If i push [reversedcidwordarray[i], amount] directly to objectchange.change. It passes the 1st and 2nd tests (return an object and returning the subarray with only one denomination).
However it seems to replace every denomination with the next one instead of pushing them all into the subarray. causing it to fail any test where the change is made up of more than 1 denomination.
That’s why i tried first storing them all as subarray in the xchange array and then pushing that to objectchange.change, but that only passes the 1st test even though it looks correct to me?

In the screen shot, do you think objectchange.change = ; could be causing the issue??

`if (roundednum >0 && amount2 >0) {

        change = change - amount;
     
      
      objectchange["status"];
      objectchange["change"];

       objectchange.status = "OPEN";
         
         xchange.push([reversedcidwordarray[i], amount]);

      
    }




}
 objectchange.change = xchange;

return objectchange `

i’ve tried removing objectchange.change =[]; like above but it still doesn’t pass the tests.

Can I have a sample input and an expected output so that I can understand your issue better? I want to help you solve this.

input
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]]}

using this code that works on the first two tests:

          objectchange["status"];
          objectchange["change"];

           objectchange.status = "OPEN";
           objectchange.change = [];
             
             objectchange.change.push([reversedcidwordarray[i], amount]);
                     
             
            
          
        }




    }
    

    return objectchange

returns this


change: Array(1)0: (2) ["PENNY", 0.04]
length: 1__proto__: Array(0)status: "OPEN"__proto__: Object

It works on the 1st test because the change to be returned is only one denomination, when there is more than one to be returned as above only the final one is stored due to push replacing the previous one each time.

so i modified the code to this:

``
objectchange[“status”];
objectchange[“change”];

       objectchange.status = "OPEN";
         
         xchange.push([reversedcidwordarray[i], amount]);
                 
         
        
      
    }




}
 objectchange.change = xchange;

return objectchange

}`

pushing the denominations and amounts as subarrays into the xchange array then having this as as the property of objectchange.change.

This returns:

change: Array(7)
0: (2) ["TWENTY", 60]
1: (2) ["TEN", 20]
2: (2) ["FIVE", 15]
3: (2) ["ONE", 1]
4: (2) ["QUARTER", 0.5]
5: (2) ["DIME", 0.2]
6: (2) ["PENNY", 0.04]
length: 7

which matches the expected output for the test so i don’t know why it isn’t passing.

Firstly, for this input:
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 condition: if (cidtotal < change) for insufficient funds will not stand true in all cases like for the above case. So you need to work one more logic for that.

I think that is what you are missing. Else most of the part looks good.

1 Like

Thank you so much! will have a go at fixing it in the morning to confirm this but looks like that was the problem! I spent so long looking in the wrong place haha.

1 Like

Just to follow up. This is the final code I came up with that passed all the tests. Thanks!

function checkCashRegister(price, cash, cid) {
  var changearray = [];
var xchange=[];
    var cidnumberarray = [];
    var cidwordarray= [];

    
      var objectchange = {status: null, change: [] };

    var change = cash - price;
    
    for (var i = 0; i < cid.length; i++) {
        for (var x = 0; x < cid.length; x++) {
            
            if (typeof cid[i][x] == "number") {
                cidnumberarray.push(cid[i][x]);



            }
            else {cidwordarray.push(cid[i][x]);}

        }
    }



    var cidtotal = cidnumberarray.reduce(function(acc, element) {
        return element + acc;
    })
    

        if (cidtotal == change) {
            console.log("total" + cidtotal)
console.log("change " + change)

            console.log("equals");
           

          objectchange.status = "CLOSED";
           
             objectchange.change = cid;

           return objectchange 

        } 

 

var coins = [
    [100],
    [20],
    [10],
    [5],
    [1],
    [0.25],
    [0.10],
    [.05],
    [.01]
];
 var filteredcidwordarray = cidwordarray.filter(function(element){
              return element != null;
            })
            var reversedcidwordarray = filteredcidwordarray.reverse();
var numberarray2 =cidnumberarray.reverse();

for (var i = 0; i < coins.length; i++) {

    var num = ((change * 100) / (coins[i] * 100));
    num  = num.toFixed(2);
 
  
    
    var roundednum = Math.floor(num);
     console.log("roundednum " +roundednum )

    var amount = roundednum * coins[i];



   while (amount > 0 && amount > numberarray2[i]){
    amount = amount - coins[i];

   }

    var amount2 = numberarray2[i] - (amount/100);



    if (roundednum >0  && amount2 >0) {

            change = change - amount;
         
          
        

           objectchange.status = "OPEN";
             
             xchange.push([reversedcidwordarray[i], amount]);
                     
             
            
          
        }}
        
if (cidtotal < change || xchange.length <1 || change > 0)
{
  objectchange.status = "INSUFFICIENT_FUNDS";


return objectchange

}



    
     objectchange.change = xchange;

    return objectchange
 
}
checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
1 Like

I am so glad to see this!

Happy coding !!

1 Like