0

Given below is the code in which I tried to modify an array using arrow function but while running, it returns a value of undefined as the output can someone give suggestions please

var modifyArray = (nums) => { return nums.forEach(function (element) { (element % 2 == 0) ? element *= 2 : element *= 3; }) } console.log(modifyArray([1,2,3,4,5]));

1
  • 1
    forEach method doesn't return any particular value, that is it will return undefined. You can try to use Array.prototype.map instead. ``` return nums.map(function (element) { (element % 2 == 0) ? element *= 2 : element *= 3; }) ``` Commented May 18, 2019 at 12:24

5 Answers 5

3

.forEach returns undefined. You probably want to .map to a new array and return that:

 return nums.map(function (element) { return element * ((element % 2 == 0) ? 2 : 3); }); 

For sure you also have to return the statement inside of the inner function, otherwise that evaluates to undefined too.

Sign up to request clarification or add additional context in comments.

Comments

0

forEach doesn't return anything - use map instead:

var modifyArray = nums => nums.map(element => element % 2 == 0 ? element *= 2 : element *= 3) console.log(modifyArray([1,2,3,4,5]));

Also note that inside of the function passed to forEach you weren't returning anything - with ES5 function you need to explicitly return - not required with ES6 =>.

1 Comment

I get your point but even if he explicitly return from inside the callback method, it still would have got nothing since forEach expects a method that returns void
0

See the answers above.

There is a workaround if you still want to use forEach

let result = [] var modifyArray = (nums) => { nums.forEach(function (element) { result.push((element % 2 == 0) ? element *= 2 : element *= 3); }) return result; } modifyArray([1,2,3,4,5]) console.log(result) 

Comments

0

What about using for loops?

var modifyArray = (nums) => { for (let i = 0; i < nums.length; i++) { let element = nums[i]; // console.log(element + ' - ' + element % 2 ); nums[i] = (element % 2 == 0) ? element *= 2 : element *= 3; } return nums; } console.log(modifyArray([1,2,3,4,5])); 

Comments

0

For each returns undefined as it doesn't return anything but you can use forEach for doing the same thing by pushing your data into an array. Notice I did not change your approach but just extended it

var modifyArray = (nums) => { var somearray = [] nums.forEach((element) => somearray.push((element % 2 == 0) ? element *= 2 : element *= 3)); return somearray; } console.log(modifyArray([1, 2, 3, 4, 5]));

Use array.push to push data to an array. If you want to use the map method you can also use that like

var modifyArray = (nums) => { return nums.map((element) => { return (element % 2 == 0) ? element *= 2 : element *= 3 }); } console.log(modifyArray([1,2,3,4,5]));

Wanna use for loops? Then you can also try this approach. It's the same as the forEach loop

var modifyArray = (nums) => { var somearray = []; for (let i = 0; i < nums.length; i++) { somearray.push((nums[i] % 2 == 0) ? nums[i] *= 2 : nums[i] *= 3) } return somearray; } console.log(modifyArray([1, 2, 3, 4, 5]));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.