-1

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)
Laurel
  • 5,965
  • 14
  • 31
  • 57
Samee_code
  • 31
  • 7
  • Welcome to SO. You really need to provide a properly detailed explanation of what you wish to accomplish as well as where you are having specific problems with the code shown – charlietfl Dec 24 '21 at 17:34
  • OK so how should TWENTY become 60 and TEN become 20? That is not clear. You can [edit] the question to include additional details at any time – charlietfl Dec 24 '21 at 17:36
  • 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 dont match values of changeArray. – Samee_code Dec 24 '21 at 17:39
  • Why unit amount is an array of arrays? It would be much better as an object. – Redu Dec 24 '21 at 17:40
  • Its like a cash register. 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 – Samee_code Dec 24 '21 at 17:41
  • 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. Im still a novice in javascript and Im trying to learn. I just need some direction. Also the exercise requires me to output the final array as a nested array. ["TWENTY",60] (like this) – Samee_code Dec 24 '21 at 17:42
  • So that "like cash register" part is missing from the explanation of what this code is actually supposed to do – charlietfl Dec 24 '21 at 17:43
  • Sorry about that, its my first time posting. Been working on this since yesterday. So i thought it was time i asked for help. This is just a portion of my whole code – Samee_code Dec 24 '21 at 17:45

1 Answers1

1

So basically what you can do here is to firstly check if a match of the element is found in the array inside UNIT_AMOUNT. If yes, you proceed to check if your newarr already has it. If yes, simply perform addition, or else you just have to push the array to your newarr.

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 change of changeArray) {
  let obj = UNIT_AMOUNT.find(amount => amount[1] == change);
  if (obj) {
    let existingObj = newarr.find(amount => amount[0] == obj[0]);
    if (existingObj)
      existingObj[1] = +parseFloat((existingObj[1] + change) + "").toFixed(2);
    else
      newarr.push([obj[0], change]);
  }
}

console.log(newarr)
Deepak
  • 2,660
  • 2
  • 8
  • 23
  • I see whats happening here, appreciate the help. However now my dimes show up as 0.30000004. I know languages have problems with decimals. So how would I fix this – Samee_code Dec 24 '21 at 17:56
  • This is a replication of what you have tried. I am not sure about your business logic. Do you wish to round it up to cut short the decimal places? [This](https://stackoverflow.com/questions/10473994/javascript-adding-decimal-numbers-issue) may be of good help. – Deepak Dec 24 '21 at 18:01
  • the dimes should output to 0.3 as we cannot give 0.300004 change back. I appreciate all your help, im just trying to get this to work perfect. Sorry to bother – Samee_code Dec 24 '21 at 18:04
  • I've updated the answer. May be you can use [toFixed()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed) combined with [parseFloat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat) to achieve what you are asking. Beware of decimal handling in JavaScript as there are multiple ways to achieve multiple things with decimal numbers ;) – Deepak Dec 24 '21 at 18:14
  • 1
    I have been studying the decimals and you are right there are many different ways to do it which is kinda annoying and hard to understand but I will get there. Thank you for the help – Samee_code Dec 24 '21 at 18:20