# JavaScript Algorithms and Data Structures Projects - Cash Register

Tell us what’s happening:
Hi everyone,
this is my code for the JS challenge " Cash Register".
I can pass other tests except for this one:
`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]]}` .

Can somebody helps me debug ? Thanks a lot.

``````function checkCashRegister(price, cash, cid) {
let change=cash-price;
let sum=0;
for (let i=0;i<cid.length;i++){
sum+=cid[i][1];
}
if(change === sum){
return {status: "CLOSED", change: cid}
} else if (change > sum){
return {status: "INSUFFICIENT_FUNDS", change: []}
} else {
let output=[];
let table=[100,20,10,5,1,0.25,0.1,0.05,0.01];
let newCid=cid.reverse();
for (let i=0;i<newCid.length;i++){
if (change<=newCid[i][1]){
output.push(parseInt(change/table[i])*table[i]);
change -= parseInt(change/table[i])*table[i];
} else {
output.push(newCid[i][1]);
change -= newCid[i][1];
}

}
if (change===0){
let result=[];
let map=[
["ONE HUNDRED", output[0]],
["TWENTY", output[1]],
["TEN", output[2]],
["FIVE", output[3]],
["ONE", output[4]],
["QUARTER", output[5]],
["DIME", output[6]],
["NICKEL", output[7]],
["PENNY", output[8]]
];
for (let i=0;i<map.length;i++){
if (map[i][1]!==0){
result.push(map[i]);
}
}
return {status: "OPEN", change: result }
}else {return {status: "INSUFFICIENT_FUNDS", change: []}
}
}

}

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

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36`

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

First question: Do you know what that test is actually returning when run against your code?

{
“status”: “INSUFFICIENT_FUNDS”,
“change”:
}

Perfect. So that tells you that this `if` statement:

`if (change === 0) {`

isn’t working as intended, because you know the answer should return `status: "OPEN"`.

Do you have any insight into why `change` doesn’t equal `0` there for this particular test? What do you think you could do to troubleshoot this?

Hi, thanks a lot. I tried to print the test result and it comes out : {
“status”: “OPEN”,
“change”: [
[
“TWENTY”,
60
],
[
“TEN”,
20
],
[
“FIVE”,
15
],
[
“ONE”,
1
],
[
“QUARTER”,
0.5
],
[
“DIME”,
0.2
],
[
“PENNY”,
0.03
]
]
}
So the problem is that the result of “Penny” becomes 0.03 while it supposed to be 0.04.

Sorry, I don’t think I’m following you. Using the code you originally pasted above, for the test case that is not passing your function is returning

``````{ status: 'INSUFFICIENT_FUNDS', change: [] }
``````

So I’m not sure where you got that “PENNY” is 0.03? Have you changed your code? If so, please paste in the newer version.

``````function checkCashRegister(price, cash, cid) {
let change=cash-price;
let sum=0;
for (let i=0;i<cid.length;i++){
sum+=cid[i][1];
}
if(change === sum){
return {status: "CLOSED", change: cid}
} else if (change > sum){
return {status: "INSUFFICIENT_FUNDS", change: []}
} else {
let output=[];
let table=[100,20,10,5,1,0.25,0.1,0.05,0.01];
let newCid=cid.reverse();
for (let i=0;i<newCid.length;i++){
if (change<=newCid[i][1]){
output.push(parseInt(change/table[i])*table[i]);
change -= parseInt(change/table[i])*table[i];
} else {
output.push(newCid[i][1]);
change -= newCid[i][1];
}

}

let result=[];
let map=[
["ONE HUNDRED", output[0]],
["TWENTY", output[1]],
["TEN", output[2]],
["FIVE", output[3]],
["ONE", output[4]],
["QUARTER", output[5]],
["DIME", output[6]],
["NICKEL", output[7]],
["PENNY", output[8]]
];
for (let i=0;i<map.length;i++){
if (map[i][1]!==0){
result.push(map[i]);
}
}
return {status: "OPEN", change: result }

}

}
``````

Sorry, I forgot to telle you that I did change a little bit of the last part of my code, just to see the result , in order to know why `if (change === 0) {`

isn’t working as intended.

What might you want to look at in order to figure this out? Since you making decisions based on the value of `change`, maybe it would be a good idea to see what the value of `change` is for this particular test? Perhaps see how the value of `change` changes as you are trying to figure out if you can make change.

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (`</>`) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

HI, thanks a lot, I added a line of code :
change = Math.round(change * 100) / 100;
after :
change -= parseInt(change/table[i])*table[i];
and the problem is solved.

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