Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

134
Visualizações
Function which increments a value by 125 from based on an array Index - javascript

I want to generate a number in increments of 125 from index 1 based on the index while looping over an array. If the index is 0, it should print 100. If the index is 1, 100 + 125 = 225, index is 2, 225 + 125 = 350 and so on. How do I do this? This is what I have.

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)

Expected output:

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

Can someone help me out with this math? Thanks.

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

const rows = 5;

const rowArray = new Array(rows).fill(' ');

const result = rowArray.map((i, index) => {
  return 100 + 125*index

})

console.log(result)

This should be better: you were multiplying 100, and not 125. Note that index===1 and index===0 are still included in 100 + 125*index

about 4 years ago · Santiago Trujillo Relatório

0

The result function can be rewritten as this:


const result = rowArray.map((i, index) => index * 125 + 100)

about 4 years ago · Santiago Trujillo Relatório

0

By the way, Array#reduce is perfect for this:

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda