# Cash Register help :(

Tell us what’s happening:
i can’t get it i don’t know why and can’t use the function or any method

``````
var money=[
{name:"ONE HUNDRED",value:100},
{name:"TWENTY",value:20},
{name:"TEN",value:10},
{name:"FIVE",value:5},
{name:"ONE",value:1},
{name:"QUARTER",value:0.25},
{name:"DIME",value:0.1},
{name:"NICKEL",value:0.05},
{name:"PENNY",value:0.01},

]

function checkCashRegister(price, cash, cid) {
var sum = 0;
var arr=[];
var num =[];
for (let i in money){
var necid = cid.reverse();
num.push(necid[i][1]/money[i].value);
}

var change = cash - price;
for (let i in cid){
sum = cid[i][1] + sum;
}
if (change > sum ){
return  {status: "INSUFFICIENT_FUNDS", change: []};
}else if (change == sum ){
return {status: "CLOSED", change: cid};

}else if(change < sum){
for(let i in money){
if ((change / money[i].value) >= 1 ){
var b =  change / money[i].value;
if(necid[i][1] - b >= 0){
arr.push([money[i].name,Math.floor(change / money[i].value)]);
change = change - (Math.floor(change / money[i].value)**money[i].value);
} else{
return arr.push([money[i].name,0]);
}

}
}
}

}

// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.1],
// ["QUARTER", 4.25],
// ["ONE", 90],
// ["FIVE", 55],
// ["TEN", 20],
// ["TWENTY", 60],
// ["ONE HUNDRED", 100]]

checkCashRegister(19.5, 20, [["PENNY", 6], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
``````

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

To solve this, you have to be more organized in how you write the code and also in your thinking.

For eg. What does this for loop do? What do you want it to do? Is it working as you expect?

Take every bit of code you wrote and ask that.

i made another code as a last try can you make it right

``````var money=[
{name:"ONE HUNDRED",value:100},
{name:"TWENTY",value:20},
{name:"TEN",value:10},
{name:"FIVE",value:5},
{name:"ONE",value:1},
{name:"QUARTER",value:0.25},
{name:"DIME",value:0.1},
{name:"NICKEL",value:0.05},
{name:"PENNY",value:0.01},

];
function checkCashRegister(price, cash, cid) {
var sum = 0;
var arr=[];
var num =[];
var necid= cid.reverse();
for (var i = 0; i < necid.length; i++){
num.push(Math.round(cid[i][1]/money[i].value)); }
// console.log( num);

var change = cash - price;

for (let i in cid){
sum = cid[i][1] + sum;
}
//  console.log(sum);
//console.log(Math.floor(0.25));
if (change > sum ){
return  {status: "INSUFFICIENT_FUNDS", change: []};

}else if(change < sum){
for(var j = 0; j <num.lenght;j++){
if (Math.floor(change/money[j].value) === 0 ){
arr.push([money[j].name,0]);
change = change;

}else if((change/money[j].value) >=1){
if(Math.floor(change/money[j].value) >= num[j]){
arr.push([money[j].name,num[j]*money[j].value]);
change = change - (num[j]*money[j].value);
}else if(Math.floor(change/money[j].value) < num[j]){
arr.push(money[j].name,(Math.floor(change/money[j].value))*money[j].value);
change = change - ((Math.floor(change/money[j].value))*money[j].value);
}
return arr , change;
}
if (change === 0 ){
return  {status: "open", change: arr.reverse()};
}else{
return  {status: "INSUFFICIENT_FUNDS", change: []};
}

// console.log(arr);

}

}else if (change === sum ){

for(var j = 0; j <num.lenght;j++){
if (Math.floor(change/money[j].value) === 0 ){
arr.push([money[j].name,0]);
change = change;

}else if((change/money[j].value) >=1){
if(Math.floor(change/money[j].value) >= num[j]){
arr.push([money[j].name,num[j]*money[j].value]);
change = change - (num[j]*money[j].value);
}else if(Math.floor(change/money[j].value) < num[j]){
arr.push(money[j].name,(Math.floor(change/money[j].value))*money[j].value);
change = change - ((Math.floor(change/money[j].value))*money[j].value);
}
return arr , change;
}
if (change === 0 ){
return  {status: "closed", change: arr.reverse()};
}else{
return  {status: "INSUFFICIENT_FUNDS", change: []};
}

}
}

}

// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.1],
// ["QUARTER", 4.25],
// ["ONE", 90],
// ["FIVE", 55],
// ["TEN", 20],
// ["TWENTY", 60],
// ["ONE HUNDRED", 100]]

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

Ok, so in the above code you are creating a num array to contain the number of coins or bills in the drawer.
however, you decided to reverse cid to do it which is a no-no since cid is an input parameter and should not be modified.
var necid = cid.reverse();
use the necid to make a deep copy of the cid array, and then reverse necid and use it instead of cid as needed.

Fixing this will be useful later as when the amount of change needed is exactly equal to the cash in drawer value you must return the original cid in the return object.

Also I notice that you have a syntax error in the code

``````      for(var j = 0; j <num.lenght;j++){
``````

you have misspelled the word ‘length’

I think you should take each failed scenario one by one and look at why it failed.
For eg. this one is failing:
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]]}.

This one happens to be the one you are checking for here:
}else if (change === sum ){

Add some console.log statements to this section of the code to see what is happening.
Also make sure you understand what is required if the change is equal to the sum.
(you need to return a status of CLOSED not ‘closed’ and you need to return the original cid array as change, again this is different than what your code is doing).

Take each failed scenario and examine the code till you find the errors.

Hope this helps get you started.