# Day 5: Arrow Functions

# Day 5: Arrow Functions

+ 44 comments For better understanding .. every new method is a modification of previous method.. This is how it helped me understand the arrow function.

//method 1

function modifyArray(nums) { var something = function(n){ if(n%2==0) return n*2; else return n*3; } var newArray = nums.map(something); return newArray; }

// method 2

function modifyArray(nums) { var something = function(n){ var a = (n%2==0) ? n*2: n*3; return a; } var newArray = nums.map(something); return newArray; }

//method 3

function modifyArray(nums) { var something = n => n = (n%2==0) ? n*2: n*3; var newArray = nums.map(something); return newArray; }

//method 4

function modifyArray(nums) { var newArray = nums.map(n => n = (n%2==0) ? n*2: n*3); return newArray; }

//method 5

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

+ 6 comments my version

let modifyArray = nums => nums.map(a => a*(2+a%2));

+ 5 comments I was surprised to see so many uses of

`map`

in this discussion. The problem repeatedly states to*modify*`nums`

. I assumed that meant`map`

was off limits as it returns a new array. However, I can see how use of`map`

would better demonstrate the value of`=>`

, but I don't see how this problem would guide anyone in that direction.

+ 1 comment This question should be given as '

**HOw to use map() method**'

+ 1 comment Using bitwise operator '&' for even/odd test on numeric input provides slight performance improvement; the Javascript solution is shown below, written in a compact form:

function modifyArray(nums) { // (n & 1) == true indicates odd number return nums.map(n => ((n & 1)? 3 : 2) * n); }

The explanation as requested: "Bitwise operator & sets each bit to 1 if both bits are 1, for example: 5 & 1 will reault in 1 (same as 0101 & 0001 = 0001)" re: JavaScript Bitwise Operations (https://www.w3schools.com/js/js_bitwise.asp)

Sort 379 Discussions, By:

Please Login in order to post a comment