We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Day 5: Arrow Functions
Day 5: Arrow Functions
+ 0 comments Basic level of coding style
function modifyArray(nums) { // nums.sort() let arr1 = [] for(let i = 0; i <= nums.length ; i++ ){ if( nums[i]%2 == 0 ){ arr1.push(nums[i]*2) // console.log("even: ",nums[i]) } else{ arr1.push(nums[i]*3) // console.log("odd: ",nums[i]) } } // console.log(arr1) for(let j = 0; j <= arr1.length; j++ ){ if(arr1[j] == undefined){ arr1.pop(arr1[j]) } } // console.log(arr1) return arr1 }
+ 0 comments function modifyArray(nums){ return nums.map(num => num % 2 === 0 ? num*2 : num*3) }
+ 0 comments function modifyArray(nums) { // Regular example with for-loop: // let newArr = []; // for (let i = 0; i < nums.length; i++) { // if (nums[i] % 2 == 0) { // newArr.push(nums[i] * 2) // } else { // newArr.push(nums[i] * 3) // } // } // Using map function to return values based on condition const newArr = nums.map(num => num % 2 === 0 ? num * 2 : num * 3); return newArr; }
+ 0 comments JavaScript
function modifyArray(nums) { return nums.map(num=>num%2?num*3:num*2) }
+ 0 comments const modifyArray = (nums) => { return nums.reduce((acc, curr)=> { curr % 2 === 0 ? acc.push(curr * 2) : acc.push(curr * 3); return acc; }, [])
}
Load more conversations
Sort 402 Discussions, By:
Please Login in order to post a comment