Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

105
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda