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

112
Vistas
Write a function that takes an array by calling another function to return a value and store it on a new array

I'm having trouble with my code for this javascript exercise as follows:

/**

  • In the function, accept an array, and call the callback on each element of the array
  • the callback will return a value - use this value and store it in a new array
  • for each iteration, the value returned from the callback, should replace the value in the previous array
  • you should return the new array with the new value
  • example:
  • const numbers = [1,2,3,4,5];
  • const newNumbers = map(numbers, (number) => {
  • return number + 3;
  • });
  • console.log(newNumbers); // [4,5,6,7,8];
  • @param {number[]} array
  • @param {Function} callback */

//my javascript code//

function map(array, callback) {
  for (let newArray of array) {
    value(callback(newArray));
  }
  return value;
}
export default map;
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can simply push callback return into array and return that same array from your custom fucntion.

You can also modify original array using index.

const customMap = (arr, callback) => {
  const finalArr = []
  arr.forEach((item, index) => {
    const res = callback(item)
    finalArr.push(res)
    arr[index] = res // This will modify original array
  })

  return finalArr
}


const numbers = [1, 2, 3, 4, 5];

const newNumbers = customMap(numbers, (number) => {
  return number + 3;
});

console.log(newNumbers);
console.log(numbers);

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