Why isn't the array being mutated in this JavaScript code?

Related searches

This is my first question on Stackoverflow :)

Below, I have set up two functions and I'm trying to use them to mutate the array "nums". But it doesn't seem to work. What am I doing wrong, especially in the last part after the function definitions? I am using higher order functions to learn how they work.

function forEach(array, callback) {
    for(let i=0; i<array.length; i++) {
    callback(array[i]);
  }
}

function mapWith(array, callback) {
    forEach(array, callback);
}

let nums = [1, 2, 3];
mapWith(nums, item => item+=2);
console.log(nums); //console output: [1, 2, 3]

But changing the last part the following way, does show that the array is changing the way I wanted. But why can't I manipulate it directly?

let nums = [];
mapWith([1, 2, 3], item => nums.push(item+=2));
console.log(nums); //console output: [3, 4, 5]

The problem here seems to be that you are not saving your returned callback value for each array element. You just need to modify your forEach() function like this

function forEach(array, callback) {
    for(let i=0; i<array.length; i++) {
        array[i] = callback(array[i]);
  }
}
Side note

Your mapWith function is doing nothing but calling your forEach function. You could just use forEach

Why Isn't 'Fall Guys' On Nintendo Switch?, 'Fall Guys' is an exclusive on PS4 and PC for now, but will likely come to Nintendo Switch and Xbox One eventually. Why Isn't California Criticized Like Florida on Covid-19? It’s a blue state, of course. But the virus doesn’t discriminate based on party affiliation. By . Joe Nocera,

It works correctly, when you call callback(array[i]); it passes value to callback function not reference, if you will pass for example an object to callback it will be passed as refernce.

An example:

const obj = {val: 1};
const modify = (obj) => {
   obj.val += 1;
}

modify(obj);
console.log(obj);

Why Isn't 'Fall Guys' On Xbox One?, With the game a massive hit on PC and PS4, why isn't Fall Guys on Xbox One yet ? When is Fall Guys coming to Xbox One? The high price is not why people aren’t running with arms open and buying EVs in droves yet; there appears to be a concerted hate campaign against them. But why, and where is it coming from?

This is what you are basically doing:

function callbackFunction (item) {
  return item + 2;
}
let test = 5;
const newNumber = callbackFunction(test);
console.log(test);
console.log(newNumber);

You don't mutate the variable test, but just return new variable. That's why you are getting the same numbers.

Also, notice you wrote this mapWith(nums, item => item=+2); instead of item += 2.

Why Isn't My Brain Working?: A revolutionary , Amazon.com: Why Isn't My Brain Working?: A revolutionary understanding of brain decline and effective strategies to recover your brain's health eBook:� On the ultraprocessed diet, the subjects on average consumed 500 more calories a day and gained two pounds. A possible reason: The participants’ levels of PYY, a hormone that suppresses appetite

Why Isn't Netflix Working, Why isn't Netflix working? If Netflix isn't working, you may be experiencing a network connectivity issue, an issue with your device, or an issue� On Sunday, Virginia Democratic Gov. Ralph Northam signed into law a measure that, among other things, makes Election Day a state holiday in the commonwealth.

Why isn't my app working?, Why isn't my app working? If you're having issues with your device or app, try these troubleshooting tricks: Try force-closing the� But that isn’t true for every interesting set of numbers. As an example, let’s look at the set of numbers of the form a + b √-5, or a +i b √5, where a and b are both integers and i is the

Why Isn't My Printer Printing? 6 printing issues you can fix. by. Tom Nelson. Writer. Tom Nelson is an engineer, programmer, network manager, and computer network and

Comments
  • You're totally right. Now I can see why it wasn't working. :) To the side note: It was part of the exercise for using higher order functions, but I understand what you're saying.