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

113
Visualizações
How can I subtract numbers from an Array to get my total to 0. I want to skip numbers that would make my total a negative

I am not done with this project, but I am stuck in one part of the project. I am having trouble just pushing the numbers that will make my total 0. For example if the change I need to give back is 5.50 my array should be [5,.50]. My numbers in my Array should not exceed the numbers inside myArr1. I might not be phrasing the question right but any insight on how to tackle this problem would be wonderful. Thank you!

function checkCashRegister(price, cash, cid) { 
  let myArr = [];
  let myChange = 0
  let change = cash-price;
  let total = cash - price
  let myArr2 = []
  let myMon =  [["PENNY", .01], ["NICKEL", .05], ["DIME",.1, ],["QUARTER", .25], ["ONE", 1],["FIVE", 5],["TEN", 10],[    "TWENTY", 20],["ONE HUNDRED", 100]]
  let myArr1 = {"PENNY":1.01, "NICKEL": 2.05,"DIME":3.1,"QUARTER": 4.25, "ONE": 90,"FIVE": 55,"TEN": 20, "TWENTY": 60,"ONE HUNDRED": 100}
  let myNum = {"PENNY":.01, "NICKEL": .05,"DIME":.1,"QUARTER": .25, "ONE": 1,"FIVE": 5,"TEN": 10, "TWENTY": 20,"ONE HUNDRED": 100}
  
for(let i =0; i <myMon.length; i++){
if(myMon[i][1] <= change){

myArr.unshift(myMon[i][1])

}
}
for(let i = 0; i<myArr.length; i++){
 myArr2.push(total -= myArr[i])
   
} 
    return myArr2

}
console.log(checkCashRegister(70, 100, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]));

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Don't work with fractions for currency. Javascript's Number type is binary-based floating point only, which means some decimal fractions aren't exactly represented. See below:

//working with fractions of Dollars
let change = 6.30
let dime = 0.10
let dimeCoins = change / dime //you won't get exactly 63 dimes
console.log(63, dimeCoins, 63 - dimeCoins)

//working with cents (no inexact rounding of fractional values) 
change = 630
dime = 10
dimeCoins = change / dime
console.log(63, dimeCoins, 63 - dimeCoins)

As for your problem model, keep in mind that for every currency denomination, the cash register has a number of units with the corresponding individual value.

When picking change, the cashier always tries to exhaust the greatest bills before moving on to the lesser, next greatest ones.

You can solve this problem using no objects, two arrays and a for loop.

about 4 years ago · Juan Pablo Isaza 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