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

138
Views
Función que incrementa un valor en 125 a partir de un índice de matriz - javascript

Quiero generar un número en incrementos de 125 desde el índice 1 basado en el índice mientras recorro una matriz. Si el índice es 0, debe imprimir 100. Si el índice es 1, 100 + 125 = 225, el índice es 2, 225 + 125 = 350 y así sucesivamente. ¿Cómo hago esto? Esto es lo que tengo.

 const rows = 5; const rowArray = new Array(rows).fill(' '); const result = rowArray.map((i, index) => { if (index === 0) { return 100 } if (index === 1) { return 225 } return index * 100 + 125 }) console.log(result)

Rendimiento esperado:

 [100, 225, 350, 475, 600, 725]

¿Alguien puede ayudarme con estas matemáticas? Gracias.

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

 const rows = 5; const rowArray = new Array(rows).fill(' '); const result = rowArray.map((i, index) => { return 100 + 125*index }) console.log(result)

Esto debería ser mejor: estabas multiplicando 100, y no 125. Ten en cuenta que index===1 e index===0 todavía están incluidos en 100 + 125*index

about 4 years ago · Santiago Trujillo Report

0

La función de result se puede reescribir así:

 const result = rowArray.map((i, index) => index * 125 + 100)
about 4 years ago · Santiago Trujillo Report

0

Por cierto, Array#reduce es perfecto para esto:

 const rows = 5; const rowArray = new Array(rows).fill(' '); const result = [100]; rowArray.reduce((a, b) => (b = a + 125, result.push(b), b), 100); console.log(result);
about 4 years ago · Santiago Trujillo 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!