Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!