Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

241
Views
Need to compile the changeArray and UNIT_AMOUNT to output new array that has the object and change. Ex-newarr=[["TWENTY",60],["TEN",20]]

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)
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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)

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!