# Building a cash machine - variable won't update

Please see below the code I have written. Despite me checking the console log and confirming the following is true (quartersDue * 0.25 <= cid[3][1]), as well as
quartersReturned = 2, the ‘change’ variable won’t update even though the logic suggests it should. I honestly can’t see why this is working and would be very grateful if someone could please assist me -

``````const changefunctcomplex = () => {let change = cashInput.value - price;
const hundredsDue = Math.floor(change / 100);
if(hundredsDue*100 <= cid[8][1]){hundredsReturned = hundredsDue; change -= hundredsReturned*100} if(hundredsDue*100 > cid[8][1]){hundredsReturned = cid[8][1] /100; change -= hundredsReturned*100 }

const twentiesDue = Math.floor((change - (100*hundredsReturned))/20);if(twentiesDue*20 <= cid[7][1]){twentiesReturned = twentiesDue; change -= twentiesReturned*20} if(twentiesDue*20 > cid[7][1]){twentiesReturned = cid[7][1] /20; change -= twentiesReturned*20}

const tensDue = Math.floor((change - (100*hundredsReturned + 20*twentiesReturned))/10);if(tensDue*10 <= cid[6][1]){tensReturned = tensDue; change -= tensReturned*10} if(tensDue*10 > cid[6][1]){tensReturned = cid[6][1] /10; change -= tensReturned*10 }

const fivesDue = Math.floor((change - (100*hundredsReturned + 20*twentiesReturned +10*tensReturned))/5);if(fivesDue*5 <= cid[5][1]){fivesReturned = fivesDue; change -= fivesReturned*5} if(fivesDue*5 > cid[5][1]){fivesReturned = cid[5][1] /5; change -= fivesReturned*5}

const onesDue = Math.floor((change - (100*hundredsReturned + 20*twentiesReturned +10*tensReturned + 5*fivesReturned ))/1);if(onesDue <= cid[4][1]){onesReturned = onesDue; change -= onesReturned} if(onesDue > cid[4][1]){onesReturned = cid[4][1]; change -= onesReturned}

const quartersDue = Math.floor((change - (100*hundredsReturned + 20*twentiesReturned +10*tensReturned + 5*fivesReturned + onesReturned))/0.25);if(quartersDue * 0.25 <= cid[3][1]){quartersReturned = quartersDue; change -= quartersReturned * 0.25} if(quartersDue * 0.25 > cid[3][1]){quartersReturned = cid[3][1] / 0.25; change -= quartersReturned * 0.25}

const dimesDue = Math.floor((change - (100*hundredsReturned + 20*twentiesReturned +10*tensReturned + 5*fivesReturned + onesReturned + 0.25 * quartersReturned))/0.1);if(dimesDue * 0.1 <= cid[2][1]){dimesReturned = dimesDue; change -= dimesReturned * 0.1} if(dimesDue * 0.1 > cid[2][1]){dimesReturned = cid[2][1] / 0.1; change -= dimesReturned * 0.1}

const nickelsDue = Math.floor((change - (100*hundredsReturned + 20*twentiesReturned +10*tensReturned + 5*fivesReturned + onesReturned + 0.25 * quartersReturned + 0.1 * dimesReturned))/0.05);if(nickelsDue * 0.05 <= cid[1][1]){nickelsReturned = nickelsDue; change -= nickelsReturned * 0.05} if(nickelsDue * 0.05 > cid[1][1]){nickelsReturned = cid[1][1] / 0.05; change -= nickelsReturned * 0.05}

const penniesDue = Math.ceil((change - (100*hundredsReturned + 20*twentiesReturned +10*tensReturned + 5*fivesReturned + onesReturned + 0.25 * quartersReturned + 0.1 * dimesReturned + 0.05 * nickelsReturned))/0.01);if(penniesDue * 0.01 <= cid[0][1]){penniesReturned = penniesDue; change -= penniesReturned * 0.01} if(penniesDue * 0.01 > cid[0][1]){penniesReturned = cid[0][1] / 0.01; change -= penniesReturned * 0.01}
``````

How do you determine it doesn’t change?

Also please provide your HTML as well, it’s needed to fully reproduce the state of working code.

good question. I logged it in the console before and after when the computation should take place and the value doesn’t change.

Hello,

Your code seems a bit complex. It’s best to follow these practices for better readability and maintenance: KISS, DRY, and YAGNI.

Writing everything in a single line makes it difficult to read and understand. Also, applying the same process for all changes can be improved by creating a function that can handle various conditions with input. Additionally, using loops can enhance readability.

Yes you are right. Just wondering why it wasn’t working and then I’ll aim to clean up after / find a more efficient way. Do you understand why the change variable isn’t updating? Thank you for the article link, I will have a read!

1 Like

No I can’t understand why it is not working because I couldn’t test it because I couldn’t understand your code logic. Try it clean and simple, probably you can see what is wrong on there before discuss .

Also, try consolidating it into a single `due` function for all changes. Consider a function that continues working until the change (input - price) reaches zero.

Edit:
Okay, I couldn’t resist myself from diving deeper into your code. First of all, one curly bracket is missing, which will cause a syntax error. There is probably more code in your project, and you’re trying to send the relevant part of it. If you send all of it, it will be more testable.

But in your `changefunctcomplex`, you’re trying to pay change one by one with `cid` elements.

For example, you’re trying to determine if this `change` is divisible by ‘100’, and even if it’s zero, you execute the if block and subtract ‘0’ from ‘change’. After that, you check if the change is more than your \$100 cash. You find the amount of all your \$100 bills with `hundredsReturned` and subtract this from the change. This same process works for each type of change.

So, even writing just the first change due takes too much effort, right? If you try to understand your code logic, you can make it easier and shorter. Also, some change functions are not working correctly, and you don’t update your `cid` balance for each change payment. For example, you pay \$100 with `hundredsReturned` and subtract it from change (`change -= hundredsReturned * 100;`), but your cash amount remains the same, \$100.

Furthermore, I made this function testable, and it gives an output, albeit incorrect. For example, `console.log(changefunctcomplex(10))` returns `5.75`. So the function seems to be stuck somewhere and doesn’t bring the change down to zero. However, it’s not easy to pinpoint the issue. You should simplify and streamline it to accommodate various conditions first.

Happy coding…

1 Like

Hi. If you were curious heres all my javascript code. The cash drawer updates externally afterwards -

``````const cashInput = document.getElementById("cash")
const changeDue = document.getElementById("change-due")
const purchaseBtn = document.getElementById("purchase-btn")

let price = 19.5;
let cid = [
["PENNY", 1.01],
["NICKEL", 2.05],
["DIME", 3.1],
["QUARTER", 4.25],
["ONE", 90],
["FIVE", 55],
["TEN", 20],
["TWENTY", 60],
["ONE HUNDRED", 100]
];
const sumNumbers = (accumulator, currentValue) => accumulator + currentValue[1];
const totalSum = cid.reduce(sumNumbers, 0);
console.log(totalSum);

purchaseBtn.addEventListener("click",() => {const cash = cashInput.value; if(cash<price){alert("Customer does not have enough money to purchase the item")} else(changefunct())})

const changefunct = () => {const cash = cashInput.value;if (cash == price){changeDue.textContent ="No change due - customer paid with exact cash"} else(changefunctcomplex())}

let hundredsReturned = ""
let twentiesReturned = ""
let tensReturned = ""
let fivesReturned = ""
let onesReturned = ""
let quartersReturned = ""
let dimesReturned = ""
let nickelsReturned = ""
let penniesReturned = ""

const changefunctcomplex = () => {let change = cashInput.value - price; console.log(change)
const hundredsDue = Math.floor(change / 100);
if(hundredsDue*100 <= cid[8][1]){hundredsReturned = hundredsDue; change -= hundredsReturned*100} if(hundredsDue*100 > cid[8][1]){hundredsReturned = cid[8][1] /100; change -= hundredsReturned*100 }

const twentiesDue = Math.floor((change - (100*hundredsReturned))/20);if(twentiesDue*20 <= cid[7][1]){twentiesReturned = twentiesDue; change -= twentiesReturned*20} if(twentiesDue*20 > cid[7][1]){twentiesReturned = cid[7][1] /20; change -= twentiesReturned*20}

const tensDue = Math.floor((change - (100*hundredsReturned + 20*twentiesReturned))/10);if(tensDue*10 <= cid[6][1]){tensReturned = tensDue; change -= tensReturned*10} if(tensDue*10 > cid[6][1]){tensReturned = cid[6][1] /10; change -= tensReturned*10 }

const fivesDue = Math.floor((change - (100*hundredsReturned + 20*twentiesReturned +10*tensReturned))/5);if(fivesDue*5 <= cid[5][1]){fivesReturned = fivesDue; change -= fivesReturned*5} if(fivesDue*5 > cid[5][1]){fivesReturned = cid[5][1] /5; change -= fivesReturned*5}

const onesDue = Math.floor((change - (100*hundredsReturned + 20*twentiesReturned +10*tensReturned + 5*fivesReturned ))/1);if(onesDue <= cid[4][1]){onesReturned = onesDue; change -= onesReturned} if(onesDue > cid[4][1]){onesReturned = cid[4][1]; change -= onesReturned}

const quartersDue = Math.floor((change - (100*hundredsReturned + 20*twentiesReturned +10*tensReturned + 5*fivesReturned + onesReturned))/0.25);if(quartersDue * 0.25 <= cid[3][1]){quartersReturned = quartersDue; change -= quartersReturned * 0.25} if(quartersDue * 0.25 > cid[3][1]){quartersReturned = cid[3][1] / 0.25; change -= quartersReturned * 0.25}

const dimesDue = Math.floor((change - (100*hundredsReturned + 20*twentiesReturned +10*tensReturned + 5*fivesReturned + onesReturned + 0.25 * quartersReturned))/0.1);if(dimesDue * 0.1 <= cid[2][1]){dimesReturned = dimesDue; change -= dimesReturned * 0.1} if(dimesDue * 0.1 > cid[2][1]){dimesReturned = cid[2][1] / 0.1; change -= dimesReturned * 0.1}

const nickelsDue = Math.floor((change - (100*hundredsReturned + 20*twentiesReturned +10*tensReturned + 5*fivesReturned + onesReturned + 0.25 * quartersReturned + 0.1 * dimesReturned))/0.05);if(nickelsDue * 0.05 <= cid[1][1]){nickelsReturned = nickelsDue; change -= nickelsReturned * 0.05} if(nickelsDue * 0.05 > cid[1][1]){nickelsReturned = cid[1][1] / 0.05; change -= nickelsReturned * 0.05}

const penniesDue = Math.ceil((change - (100*hundredsReturned + 20*twentiesReturned +10*tensReturned + 5*fivesReturned + onesReturned + 0.25 * quartersReturned + 0.1 * dimesReturned + 0.05 * nickelsReturned))/0.01);if(penniesDue * 0.01 <= cid[0][1]){penniesReturned = penniesDue; change -= penniesReturned * 0.01} if(penniesDue * 0.01 > cid[0][1]){penniesReturned = cid[0][1] / 0.01; change -= penniesReturned * 0.01}

cid[0][1] -= penniesReturned/100;
cid[1][1] -= nickelsReturned/20;
cid[2][1] -= dimesReturned/10;
cid[3][1] -= quartersReturned/4;
cid[4][1] -= onesReturned;
cid[5][1] -= fivesReturned * 5;
cid[6][1] -= tensReturned * 10;
cid[7][1] -= twentiesReturned * 20;
cid[8][1] -= hundredsReturned * 100;

console.log(quartersReturned)
console.log(quartersDue * 0.25 < cid[3][1])
console.log(change)

const total = cid.reduce((acc, current) => acc + current[1], 0);

let changeText=""
const calculator = () => {
if(hundredsDue > 0){
changeText = changeText.concat(` ONE HUNDRED: \$\${hundredsDue * 100}`)
}
if(twentiesDue > 0){
changeText = changeText.concat(` TWENTY: \$\${twentiesDue * 20}`)
}
if(tensDue > 0){
changeText = changeText.concat(` TEN: \$\${tensDue * 10}`)
}
if(fivesDue > 0){
changeText = changeText.concat(` FIVE: \$\${fivesDue * 5}`)
}
if(onesDue > 0){
changeText = changeText.concat(` ONE: \$\${onesDue}`)
}
if(quartersDue > 0){
changeText = changeText.concat(` QUARTER: \$\${quartersDue * 0.25}`)
}
if(dimesDue > 0){
changeText = changeText.concat(` DIME: \$\${dimesDue * 0.1}`)
}
if(nickelsDue > 0){
changeText = changeText.concat(` NICKEL: \$\${nickelsDue * 0.05}`)
}
if(penniesDue > 0){
changeText = changeText.concat(` PENNY: \$\${penniesDue * 0.01}`)
}
}
calculator()
if(total === 0){changeDue.textContent = `Status: CLOSED\${changeText}`}
if(total > 0){changeDue.textContent = `Status: OPEN\${changeText}`}
if(change > 0){changeDue.textContent = "Status: INSUFFICIENT_FUNDS";
cid[0][1] += penniesReturned/100;
cid[1][1] += nickelsReturned/20;
cid[2][1] += dimesReturned/10;
cid[3][1] += quartersReturned/4;
cid[4][1] += onesReturned;
cid[5][1] += fivesReturned * 5;
cid[6][1] += tensReturned * 10;
cid[7][1] += twentiesReturned * 20;
cid[8][1] += hundredsReturned * 100;}

}
``````

These variables are declared as strings, it gives wrong result sum with your number variable `change` if you want to make them number, you can declare zero for them. But they are very unneccessary.

Also, your `change` variable needs to be rounded to two decimal places. You can use `.toFixed(2)` but don’t forget it return string. You should parse to float after this.

To improve your code and identify the real issue, please format your code first, everything is one line is very hard to understand. If you using VS Code, you can use `prettier` or if you using codePen you can format your code in dropdown option.

After that, compare each `...Due` function and try to understand what is changing and what remains the same. Sometimes, I also write functions in a lengthy manner initially, but then I refactor them to be shorter. You will learn create more dynamic functions, it is okay. The repeating parts can always be placed inside a loop, while the changing parts can be handled as variables or prop.

And your `calculator` function is so repeat it self, you can improve it too. Or you can write a single line in your new single `due` function.

1 Like

Wow thank you so much for your help. It must be the fact I was trying to sum strings… . Still making basic errors but I am quite new! (just finishing the JS course on FCC)). I will try and get this working by summing numbers and then work on the efficiency as you described.

You’re welcome.
Making mistakes and creating bugs are inherent to software development. There’s nothing wrong here. That’s why keeping an eye on the documentation at all times and writing clean code to facilitate debugging is crucial.
Good luck!..