Sort by

recency

|

442 Discussions

|

  • + 0 comments
    const modifyArray = (nums) => {
        return nums.map((num)=>{return (num%2 === 0)?(num*2):(num*3)})
    }
    

    this is the simpliest way to do it , where map() creates a new array from calling a function for every array element and does not change the original array. then just return the new array for modifyArray function.

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

    In JS

    function modifyArray(nums) {
        const res = nums.map((val) => {
      if (val % 2 == 0) {
        return val * 2;
      } else {
        return val * 3;
      }
    });
    
    return res;
    
    }
    
  • + 0 comments

    Managed to fit in one line:

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