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

177
Vistas
minimum of list to maximum of value in Javascript

I currently have a list in Javascript:

let x = [1.0, 2.5, 5.0, 20.0, 50.0, 100.0, 500.0, 2000.0, 5000.0] where each value of this is a dollar coin.

My problem is the following, I need some equation for Javascript to choose the smallest possible number of coins to reach the maximum in the desired value.

I'll give an example:

I need as few coins as possible to max out at 5.5USD. I would use a coin of 2.5USD and 3 coins of 1.0USD to arrive at 5.5USD.

Restriction: The value obtained cannot exceed the desired value.

What math function would I use for this? Or does anyone know a technical name given to this type of equation for me to look up? Well, there was never anything like it.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You have to write logic , no inbuilt function as such can be done easily by reduce !

let a = [1.0, 2.5, 5.0, 20.0, 50.0, 100.0, 500.0, 2000.0, 5000.0];

let query = 80.0;

while (query > 0) {
  //find closest maximum number
  const output = a.filter(t => t <= query).reduce((prev, curr) => Math.abs(curr - query) < Math.abs(prev - query) ? curr : prev);

  query = query - output;
  console.log(output);
}

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