Cash register code does something strange

Somewhere along the line in cidObject (which is a conversion of the given array into an object) TEN gets a value of ‘-10’ even though in the while() I specify to stop the while function if cidObject.TEN is going to below 0; because of that the end result, FIVE has an incorrect value 5, which should be 15 and I don’t see the error

``````let typesOfCash =
{"PENNY"          :       0.01 ,
"NICKEL"         :       0.05 ,
"DIME"           :       0.1  ,
"QUARTER"        :       0.25 ,
"ONE"            :       1    ,
"FIVE"           :       5    ,
"TEN"            :       10   ,
"TWENTY"         :       20   ,
"ONE HUNDRED"    :       100 };

function checkCashRegister(price, cash, cid) {

let changeToBeReturned = cash - price;
let cidObject = Object.fromEntries(cid);
let result = {
status: null,
change: []
}
console.log(cidObject);

let testArray = Object.keys(result.change);
console.log(testArray)
let totalCash = 0;
for (let i = 0; i < cid.length; i++) {
totalCash += cid[i][1];
}

if (changeToBeReturned > totalCash) {
result.status = 'INSUFFICIENT_FUNDS';
return result;
}
if (changeToBeReturned == totalCash) {
result.status = 'CLOSED';
result.change = cid;
return result;
}

while (changeToBeReturned >= typesOfCash['ONE HUNDRED'] && cidObject['ONE HUNDRED'] > 0) {
if (testArray.indexOf('ONE HUNDRED') < 0) {
testArray.push('ONE HUNDRED');
result.change.push(['ONE HUNDRED', 100]);
changeToBeReturned -= 100;
cidObject['ONE HUNDRED'] -= 100;
} else {
for (let a = 0; a < result.change.length; a++) {
if (result.change[a][0] == 'ONE HUNDRED') {result.change[a][1] += 100}
changeToBeReturned -= 100;
cidObject['ONE HUNDRED'] -= 100;
}
}}
while (changeToBeReturned >= typesOfCash.TWENTY && cidObject.TWENTY > 0) {
if (testArray.indexOf('TWENTY') < 0) {
testArray.push('TWENTY');
result.change.push(['TWENTY', 20]);
changeToBeReturned -= 20;
cidObject.TWENTY -= 20;
} else {
for (let a = 0; a < result.change.length; a++) {
if (result.change[a][0] == 'TWENTY') {result.change[a][1] += 20}
changeToBeReturned -= 20;
cidObject.TWENTY -= 20;
}
}}
while (changeToBeReturned >= typesOfCash.TEN && cidObject.TEN > 0) {
if (testArray.indexOf('TEN') < 0) {
testArray.push('TEN');
result.change.push(['TEN', 10]);
changeToBeReturned -= 10;
cidObject.TEN -= 10;
} else {
for (let a = 0; a < result.change.length; a++) {
if (result.change[a][0] == 'TEN') {result.change[a][1] += 10}
changeToBeReturned -= 10;
cidObject.TEN -= 10;
}
}}

while (changeToBeReturned >= typesOfCash.FIVE && cidObject.FIVE > 0) {
if (testArray.indexOf('FIVE') < 0) {
testArray.push('FIVE');
result.change.push(['FIVE', 5]);
changeToBeReturned -= 5;
cidObject.FIVE -= 5;
} else {
for (let a = 0; a < result.change.length; a++) {
if (result.change[a][0] == 'FIVE') {result.change[a][1] += 5}
changeToBeReturned -= 5;
cidObject.FIVE -= 5;
}
}}

while (changeToBeReturned >= typesOfCash.ONE && cidObject.ONE > 0) {
if (testArray.indexOf('ONE') < 0) {
testArray.push('ONE');
result.change.push(['ONE', 1]);
changeToBeReturned -= 1;
cidObject.ONE -= 1;
} else {
for (let a = 0; a < result.change.length; a++) {
if (result.change[a][0] == 'ONE') {result.change[a][1] += 1}
changeToBeReturned -= 1;
cidObject.ONE -= 1;
}
}}
while (changeToBeReturned >= typesOfCash.QUARTER && cidObject.QUARTER > 0) {
if (testArray.indexOf('QUARTER') < 0) {
testArray.push('QUARTER');
result.change.push(['QUARTER', 0.25]);
changeToBeReturned -= 0.25;
cidObject.QUARTER -= 0.25;
} else {
for (let a = 0; a < result.change.length; a++) {
if (result.change[a][0] == 'QUARTER') {result.change[a][1] += 0.25}
changeToBeReturned -= 0.25;
cidObject.QUARTER -= 0.25;
}
}}
while (changeToBeReturned >= typesOfCash.NICKEL && cidObject.NICKEL > 0) {
if (testArray.indexOf('NICKEL') < 0) {
testArray.push('NICKEL');
result.change.push(['NICKEL', 0.05]);
changeToBeReturned -= 0.05;
cidObject.NICKEL -= 0.05;
} else {
for (let a = 0; a < result.change.length; a++) {
if (result.change[a][0] == 'NICKEL') {result.change[a][1] += 0.05}
changeToBeReturned -= 0.05;
cidObject.NICKEL -= 0.05;
}
}}
while (changeToBeReturned >= typesOfCash.PENNY && cidObject.PENNY > 0) {
if (testArray.indexOf('PENNY') < 0) {
testArray.push('PENNY');
result.change.push(['PENNY', 0.01]);
changeToBeReturned -= 0.01;
cidObject.PENNY -= 0.01;
} else {
for (let a = 0; a < result.change.length; a++) {
if (result.change[a][0] == 'PENNY') {result.change[a][1] += 0.01}
changeToBeReturned -= 0.01;
cidObject.PENNY -= 0.01;
}
}}

if (changeToBeReturned > 0) {
result.status = 'INSUFFICIENT_FUNDS';
result.change = [];
return result;
} else {
result.status = 'OPEN';
return result;
}
}

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 @ jikiamerabs and welcome to the forum!

Look at this part of your code (I’ve reformatted it slightly) :

``````for (let a = 0; a < result.change.length; a++) {
if (result.change[a][0] == 'TEN') {
result.change[a][1] += 10
}
changeToBeReturned -= 10;
cidObject.TEN -= 10;
}
``````

What will happen whenever ` result.change[a][0]` is not equal to ‘TEN’ ?

Try adding some `console.log()` statements to help you think this one through.

1 Like

Thank you, that was helpful, but now I have a new issue, and I will stab somebody. the values in end result are NaN, except for ‘ONE’. What did I do in life to deserve this.

``````let typesOfCash =
{"PENNY"          :       0.01 ,
"NICKEL"         :       0.05 ,
"DIME"           :       0.1  ,
"QUARTER"        :       0.25 ,
"ONE"            :       1    ,
"FIVE"           :       5    ,
"TEN"            :       10   ,
"TWENTY"         :       20   ,
"ONE HUNDRED"    :       100 };

function plus(a, b) {
return ((a * 100).toFixed(2) + (b * 100).toFixed(2))/100;
}

function minus(a, b) {
return ((a * 100).toFixed(2) - (b * 100).toFixed(2))/100;
}

function checkCashRegister(price, cash, cid) {

let changeToBeReturned = minus(cash, price);
let cidObject = Object.fromEntries(cid);
let result = {
status: null,
change: []
}

let testArray = Object.keys(result.change);
let totalCash = 0;
for (let i = 0; i < cid.length; i++) {
totalCash = plus(totalCash, cid[i][1]);
}

if (changeToBeReturned > totalCash) {
result.status = 'INSUFFICIENT_FUNDS';
return result;
}
if (changeToBeReturned == totalCash) {
result.status = 'CLOSED';
result.change = cid;
return result;
}

while (changeToBeReturned >= typesOfCash['ONE HUNDRED'] && cidObject['ONE HUNDRED'] > 0) {
if (testArray.indexOf('ONE HUNDRED') < 0) {
testArray.push('ONE HUNDRED');
result.change.push(['ONE HUNDRED', 100]);
changeToBeReturned = minus(changeToBeReturned, 100)
cidObject['ONE HUNDRED'] = minus(cidObject['ONE HUNDRED'], 100)
} else {
for (let a = 0; a < result.change.length; a++) {
if (result.change[a][0] == 'ONE HUNDRED') {
result.change[a][1] = plus(result.change[a][1], 100);
changeToBeReturned = minus(changeToBeReturned, 100)
cidObject['ONE HUNDRED'] = minus(cidObject['ONE HUNDRED'], 100)
}
}
}
}
console.log(result.change)
while (changeToBeReturned >= typesOfCash.TWENTY && cidObject.TWENTY > 0) {
if (testArray.indexOf('TWENTY') < 0) {
testArray.push('TWENTY');
result.change.push(['TWENTY', 20]);
changeToBeReturned = minus(changeToBeReturned, 20);
cidObject.TWENTY = minus(cidObject.TWENTY, 20);
} else {
for (let a = 0; a < result.change.length; a++) {
if (result.change[a][0] == 'TWENTY') {
result.change[a][1] = plus(result.change[a][1], 20);
changeToBeReturned = minus(changeToBeReturned, 20);
cidObject.TWENTY = minus(cidObject.TWENTY, 20);
}
}
}
}

while ((changeToBeReturned >= typesOfCash.TEN) && (cidObject.TEN > 0)) {
if (testArray.indexOf('TEN') < 0) {
testArray.push('TEN');
result.change.push(['TEN', 10]);
changeToBeReturned = minus(changeToBeReturned, 10);
cidObject.TEN = minus(cidObject.TEN, 10);
} else {
for (let a = 0; a < result.change.length; a++) {
if (result.change[a][0] == 'TEN') {
result.change[a][1] = plus(result.change[a][1], 10);
changeToBeReturned = minus(changeToBeReturned, 10);
cidObject.TEN = minus(cidObject.TEN, 10);
}
}
}
}

while (changeToBeReturned >= typesOfCash.FIVE && cidObject.FIVE > 0) {
if (testArray.indexOf('FIVE') < 0) {
testArray.push('FIVE');
result.change.push(['FIVE', 5]);
changeToBeReturned = minus(changeToBeReturned, 5);
cidObject.FIVE = minus(cidObject.FIVE, 5);
} else {
for (let a = 0; a < result.change.length; a++) {
if (result.change[a][0] == 'FIVE') {
result.change[a][1] = plus(result.change[a][1], 5);
changeToBeReturned = minus(changeToBeReturned, 5);
cidObject.FIVE = minus(cidObject.FIVE, 5);
}
}
}
}

while (changeToBeReturned >= typesOfCash.ONE && cidObject.ONE > 0) {
if (testArray.indexOf('ONE') < 0) {
testArray.push('ONE');
result.change.push(['ONE', 1]);
changeToBeReturned = minus(changeToBeReturned, 1);
cidObject.ONE = minus(cidObject.ONE, 1);
} else {
for (let a = 0; a < result.change.length; a++) {
if (result.change[a][0] == 'ONE') {
result.change[a][1] = plus(result.change[a][1], 1);
changeToBeReturned = minus(changeToBeReturned, 1);
cidObject.ONE = minus(cidObject.ONE, 1);
}

}
}
}

while (changeToBeReturned >= typesOfCash.QUARTER && cidObject.QUARTER > 0) {
if (testArray.indexOf('QUARTER') < 0) {
testArray.push('QUARTER');
result.change.push(['QUARTER', 0.25]);
changeToBeReturned = minus(changeToBeReturned, 0.25);
cidObject.QUARTER = minus(cidObject.QUARTER, 0.25);
} else {
for (let a = 0; a < result.change.length; a++) {
if (result.change[a][0] == 'QUARTER') {
result.change[a][1] = plus(result.change[a][1], 0.25);
changeToBeReturned = minus(changeToBeReturned, 0.25);
cidObject.QUARTER = minus(cidObject.QUARTER, 0.25);
}
}
}
}

while (changeToBeReturned >= typesOfCash.DIME && cidObject.DIME > 0) {
if (testArray.indexOf('DIME') < 0) {
testArray.push('DIME');
result.change.push(['DIME', 0.1]);
changeToBeReturned = minus(changeToBeReturned, 0.1);
cidObject.DIME = minus(cidObject.DIME, 0.1);
} else {
for (let a = 0; a < result.change.length; a++) {
if (result.change[a][0] == 'DIME') {
result.change[a][1] = plus((result.change[a][1]), 0.1);
changeToBeReturned = minus(changeToBeReturned, 0.1);
cidObject.DIME = minus(cidObject.DIME, 0.1);
}
}
}
}

while (changeToBeReturned >= typesOfCash.NICKEL && cidObject.NICKEL > 0) {
if (testArray.indexOf('NICKEL') < 0) {
testArray.push('NICKEL');
result.change.push(['NICKEL', 0.05]);
changeToBeReturned = minus(changeToBeReturned, 0.05);
cidObject.NICKEL = minus(cidObject.NICKEL, 0.05);
} else {
for (let a = 0; a < result.change.length; a++) {
if (result.change[a][0] == 'NICKEL') {
result.change[a][1] = plus(result.change[a][1], 0.05);
changeToBeReturned = minus(changeToBeReturned, 0.05);
cidObject.NICKEL = minus(cidObject.NICKEL, 0.05);
}
}
}
}

while (changeToBeReturned >= typesOfCash.PENNY && cidObject.PENNY > 0) {
if (testArray.indexOf('PENNY') < 0) {
testArray.push('PENNY');
result.change.push(['PENNY', 0.01]);
changeToBeReturned = minus(changeToBeReturned, 0.01);
cidObject.PENNY = minus(cidObject.PENNY, 0.01);
} else {
for (let a = 0; a < result.change.length; a++) {
if (result.change[a][0] == 'PENNY') {
result.change[a][1] = plus(result.change[a][1], 0.01);
changeToBeReturned = minus(changeToBeReturned, 0.01);
cidObject.PENNY = minus(cidObject.PENNY, 0.01);
}
}
}
}
if (changeToBeReturned > 0) {
result.status = 'INSUFFICIENT_FUNDS';
result.change = [];
return result;
} else {
result.status = 'OPEN';
return result;
}

}

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

``````

The problem is in your `plus` function - it’s returning `Nan`.

The `toFixed()` function returns a string, so the plus sign is interpreted as string concatenation.

Try doing the whole calculation , then just calling `toFixed(2)` on the result.

1 Like

I love you, thank you

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