Question: i'm having trouble to get demanded answer the question is Create a function called forEach that takes two inputs: - an array of numbers (a

i'm having trouble to get demanded answer

the question is

Create a function called forEach that takes two inputs:

- an array of numbers (a list of numbers)

- a 'callback' function

forEach runs the callback on each element of the array (updates the array passed in). forEach does not return anything. (8 pts.)

myarray = [2,3,4]; forEach(myarray, increment); console.log(myarray); // should output [3, 4, 5] (3 pts.)

so i made,

const increment = (x) => x + 1; // increment function. i must use the arrow function here.

function forEach(array, callback) {

for (let i = 0; i < array.length; i++){

callback(array[i]);

}

}

myarray = [2,3,4]; // should output [3, 4, 5] (3 pts.)

forEach(myarray, increment);

console.log(myarray);

but i still get the answer [2,3,4] not [3,4,5]

means increment function not used and answer not changed.

i need to implement the foreach.

please find explain where my problem is with the code

and please give the correct code and result. thank you

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!