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

111
Visualizações
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 Respostas
Responde à pergunta

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 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