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
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 Respostas
Responde à pergunta

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 Relatório

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