I have code that should emulate a cash register.
I need help with the loop. I am trying to get a new array that has updated UNIT_AMOUNT values. Since my changeArray has 3 values that are $20. I want to add those 3 values and update the UNIT_AMOUNT array object to ["TWENTY", 60]
and push it to the new array. Similarly for all other instances of changeArray. My newarr should not have any objects that don't match values of changeArray.
The changeArray is the change I need to hand back to the customer. So I need a new array that has the type of bill and the total amount. I did have it as an Object in the beginning, but I was running into trouble trying to manipulate it. So I changed it to a nested array.
The exercise requires me to output the final array as a nested array: ["TWENTY",60]
.
let changeArray = [20, 20, 20, 10, 10, 5, 5, 5, 1, 0.25, 0.25, 0.1, 0.1, 0.1, 0.05, 0.01, 0.01, 0.01, 0.01]
const UNIT_AMOUNT = [ ["ONE HUNDRED", 100.00], ["TWENTY", 20.00], ["TEN", 10.00], ["FIVE", 5.00], ["ONE", 1.00], ["QUARTER", .25], ["DIME", .10], ["NICKEL", .05], ["PENNY", .01] ]
let newarr = []
for (let i = 0; i < changeArray.length; i++) {
for (let element of UNIT_AMOUNT) { if (changeArray[i] == element[1]) {
if (changeArray[i] == changeArray[i + 1]) {
element[1] = element[1] + changeArray[i]
} newarr.push(element)
}
}
console.log(newarr)