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

126
Visualizações
with JavaScript i want to have the following output

Output an array where a value is added to the next value of the array. The Last value will be added with the first value.

Example: [45, 4, 9, 16, 25] Converted to: [49, 13, 25, 41, 70]

Map() method must be used.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can try following:

console.log([45, 4, 9, 16, 25].map((item, index, array) => item + array[(index + 1) % array.length]))

about 4 years ago · Juan Pablo Isaza Relatório

0

When using Array.prototype.map() you can use the index parameter and in each iteration
to see if it's the end of the array or not like so:

const arr = [45, 4, 9, 16, 25];

const newArr = arr.map((item, i) => {
  return i !== (arr.length - 1) ? item + arr[i + 1] : item + arr[0];
})

console.log(newArr);

Array.prototype.map()

about 4 years ago · Juan Pablo Isaza Relatório

0

This works fine.

const nums = [45, 4, 9, 16, 25];

const newNums = nums.map((item, index) => {
  if(index < nums.length - 1) {
    return item + nums[ index + 1]
  }
    return item + nums[0]
})

console.log(newNums) // [ 49, 13, 25, 41, 70 ]
about 4 years ago · Juan Pablo Isaza 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