Day 5: Arrow Functions ( 10 Days of Javascript)

Task:

Complete the function in the editor. It has one parameter: an array, nums. It must iterate through the array performing one of the following actions on each element:

  • If the element is even, multiply the element by 2.
  • If the element is odd, multiply the element by 3.

The function must then return the modified array.


Solution:

/*
 * Modify and return the array so that all even elements are doubled and all odd elements are tripled.
 * 
 * Parameter(s):
 * nums: An array of numbers.
 */

function modifyArray(nums) {
    var test = function(num){
        let a;
        if(num%2==0)
            a = num*2;
        else
            a = num*3;
            
        return a;
    }
    return nums.map(test); 
    
}

Leave a Reply