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

214
Views
cómo calcular el precio usando el control deslizante [problema algorítmico]

tengo un control deslizante que permite al usuario elegir entre 0 y 20000 cómo calcular el precio final con precios por 100 puntos son así:

 from 0 to 1200 -> 2.10$/100pts from 1200 to 2400 ->2.20$/100pts from 2400 to 4800 ->2.5$/100pts from 4800 to 7200 ->3.75$/100pts from 7200 to 10000 ->5.4$/100pts from 10000 to 20000 ->10$/100pts

Ejemplo: el usuario elige entre 3000 y 12000, el precio será 45 $ + 90 $ + 151,2 $ + 200 $ => el precio final será = 486,2 $

si ustedes pueden dar una solución en javascript o estaré bien con una solución algorítmica, gracias

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Aquí hay un algoritmo que puede convertir a código:

  1. Tengamos una matriz anidada, o una matriz de objetos de precios (es importante que sean indexables y ordenados de forma ascendente), para los precios llamados prices ordenados de forma ascendente de la siguiente manera:
 [[1200, 2400, 2.2],...]
  1. Tengamos los límites inferior y superior de la entrada del usuario en las variables lower y upper .
  2. Tengamos una variable finalPrice inicializada en 0 .
  3. Encuentre el índice del primer elemento en precios donde su límite superior es > lower . Llamemos a este índice i .
  4. Ahora ejecute while lower < upper:
 // calc the cost of this range finalPrice += (min(prices[i][1], upper) - lower)/100 * prices[i] // sets `lower` to the upper bound of the previous price, // since we already calculated that. lower = min(prices[i][1], upper) // increment i to calc with the next price range i += 1

Dado que el control deslizante está restringido a 20000 en la parte superior, puede estar seguro de i nunca se sale de los límites.

about 4 years ago · Juan Pablo Isaza Report

0

Cree una tabla con los umbrales y luego, en JS, itere sobre ella para acumular el precio.

Aquí hay un fragmento ejecutable:

 const table = [ [10000, 10.00], [ 7200, 5.40], [ 4800, 3.75], [ 2400, 2.50], [ 1200, 2.20], [ 0, 2.10], ]; function convert(points) { let total = 0; for (let [limit, price] of table) { if (points > limit) { total += Math.floor((points - limit) / 100) * price; points = limit; } } return total; } // IO handling let [rngStart, rngEnd] = document.querySelectorAll("input"); let output = document.querySelector("span"); rngStart.addEventListener("input", refresh); rngEnd.addEventListener("input", refresh); function refresh() { let start = +rngStart.value; let end = +rngEnd.value output.textContent = (convert(end) - convert(start)).toFixed(2); } refresh();
 input[type=range] { width: 80% }
 From: <input type="number" min="0" max="20000" step="100" value="3000"> To: <input type="number" min="0" max="20000" step="100" value="12000"> Price: <span></span>

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!