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}