Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

117
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda