Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!