JavaScript Algorithms and Data Structures Projects - Cash Register

Hello guys, this is my first time posting for help, and it’s because I’ve seen that this problem has already been solved, but I want to know why the code I wrote doesn’t work only with one specific example, I mean, I’m doing the last test of the JavaScript course’s cash register and when I pass the following parameters: ‘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]]);’, it doesn’t return the expected result, I would like to know if they can help me please, and forgive me if I’m overlooking something, below is my code:

``````function checkCashRegister(price, cash, cid) {

var change = cash - price;
var totalCid = 0;
var changeGiven = [];

var denominations = [
{ 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.10 },
{ name: "NICKEL", value: 0.05 },
{ name: "PENNY", value: 0.01 }
];

for (var i = 0; i < cid.length; i++) {
totalCid += cid[i][1];
}

totalCid = Math.round(totalCid * 100) / 100;

if (change > totalCid) {
return { status: "INSUFFICIENT_FUNDS", change: [] };
}

if (change === totalCid) {
return { status: "CLOSED", change: cid };
}

for (var j = 0; j < denominations.length; j++) {
var denomination = denominations[j];
var denominationTotal = 0;

while (change >= denomination.value && cid[j][1] >= denomination.value) {
change -= denomination.value;
cid[j][1] -= denomination.value;
denominationTotal += denomination.value;
change = Math.round(change * 100) / 100;
}

if (denominationTotal > 0) {
changeGiven.push([denomination.name, denominationTotal]);
}
}

if (change !== 0) {
return { status: "INSUFFICIENT_FUNDS", change: [] };
}

return { status: "OPEN", change: changeGiven };
}
``````

But when i use those parameters, the function give me back this:
{ status: ‘OPEN’,
change:
[ [ ‘ONE’, 90 ],
[ ‘QUARTER’, 6.5 ],
[ ‘DIME’, 0.2 ],
[ ‘PENNY’, 0.04 ] ] }

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

Challenge: JavaScript Algorithms and Data Structures Projects - Cash Register

Link to the challenge:

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 (').

here’s your code with many console.log statements added to it.
If you copy this and run it, you can clearly see the problem I think

``````function checkCashRegister(price, cash, cid) {

var change = cash - price;
var totalCid = 0;
var changeGiven = [];

var denominations = [
{ 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.10 },
{ name: "NICKEL", value: 0.05 },
{ name: "PENNY", value: 0.01 }
];

for (var i = 0; i < cid.length; i++) {
totalCid += cid[i][1];
}

totalCid = Math.round(totalCid * 100) / 100;
console.log("totalCid after round:" + totalCid);

if (change > totalCid) {
return { status: "INSUFFICIENT_FUNDS", change: [] };
}

if (change === totalCid) {
return { status: "CLOSED", change: cid };
}

for (var j = 0; j < denominations.length; j++) {
var denomination = denominations[j];
var denominationTotal = 0;
console.log("denomination.name: " + denomination.name);
console.log("denomination.value: " + denomination.value);
console.log("change: " + change);
console.log("cid[j]: " + cid[j])
while (change >= denomination.value && cid[j][1] >= denomination.value) {
console.log("change: " + change);
change -= denomination.value;
cid[j][1] -= denomination.value;
denominationTotal += denomination.value;
change = Math.round(change * 100) / 100;
}

if (denominationTotal > 0) {
changeGiven.push([denomination.name, denominationTotal]);
}
}

if (change !== 0) {
return { status: "INSUFFICIENT_FUNDS", change: [] };
}

return { status: "OPEN", change: changeGiven };
}
console.log(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]]));

``````

Hi, sorry this my first time posting a request, thank you so much for help!

But i still a little bit confuse, you’ve change the code to see the problem “detected” at the console?

In my previous post, I provided your code back to you with the addition of many console.log statements.
If you copy and run the code those statements will log
and I think that by looking at the output in the console you can clearly see a problem in the logic.

Thank you so much!, i hope i can fix the problem soon.

1 Like

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