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

197
Visualizações
Why "change" variable get weird results

why the function doesn't work, pretty straightforward, change needs to get to 0.
i presume will be another more efficient way to solve this problem, but whats bug me is the fact that "change" get weird numbers.....help.

 function checkCashRegister(price, cash, cid) {
      var change= cash-price
      var money={
        'PENNY':0.01,
        'NICKEL':0.05,
        'DIME':0.1,
        'QUARTER':0.25,
        'ONE':1,
        'FIVE':5,
        'TEN':10,
        'TWENTY':20,
        'ONE HUNDRED':100}
        console.log(change,'start')
        let i=8;
      while(change>0){
    
    // console.log(cid[i][1])
      if(change<cid[i][1]&&change>money[cid[i][0]]){
        console.log(cid[i][0], 'cid')  
        console.log(money[cid[i][0]],'money')
        change=change-money[cid[i][0]] 
        console.log(change, 'change') 
        i=8;    
  }  
  i--
  }
  
}

checkCashRegister(19.4, 20,
[["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

JS save a float value as binary data, so your 19.4 will not be exactly 19.4. That's why 20 - 19.4 is not exactly 0.6.

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