Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

245
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda