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

127
Vistas
Aarray.map return value does not makes sense

function copyArrayAndManipulate(array, instructions) {
  return array.map(function (element, index, arr) {
    return arr.push(instructions(element));
  });
}
function multiplyBy2(input) {
 return input * 2;
}
var result = copyArrayAndManipulate([1, 2, 3], multiplyBy2);
console.log(result)

I do not know why result equals to [4,5,6], i debugged it but i can't see the value 5 even while debugging. Just trying to understand how this [4,5,6] array came from, not asking for some solution.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

map method will return you a new array. So you should not push to arr inside map method. You already get multiplied array

  function copyArrayAndManipulate(array, instructions) {
    return array.map(function (element, index, arr) {
      return instructions(element); // you should not push
    });
  }
  function multiplyBy2(input) {
    return input * 2;
  }
  var result = copyArrayAndManipulate([1, 2, 3], multiplyBy2);
about 4 years ago · Juan Pablo Isaza Denunciar

0

You are basically rewriting map using map. map already does what you call copyArrayAndManipulate, so it seems like a useless wrap around a function that already exists. If you want your code to work, you simply need to remove arr.push and just return instructions(element) within the map function. The map function will return a new "copy" of the array and "manipulate" the array with the function you give it, passing you the each "element".

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