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.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Tutorials
  3. 10 Days of Javascript
  4. Day 5: Arrow Functions
  5. Discussions

Day 5: Arrow Functions

Problem
Submissions
Leaderboard
Discussions
Editorial
Topics

Sort 402 Discussions, By:

recency

Please Login in order to post a comment

  • raj_shekhar_2021
    2 weeks ago+ 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|
    Permalink
  • orhankagan33
    1 month ago+ 0 comments
    function modifyArray(nums){
       return nums.map(num => num % 2 === 0 ? num*2 : num*3)
    }
    
    0|
    Permalink
  • celaleddinn
    1 month ago+ 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|
    Permalink
  • xXxAboodxXx
    2 months ago+ 0 comments

    JavaScript

    function modifyArray(nums) {
        return nums.map(num=>num%2?num*3:num*2)
    }
    
    0|
    Permalink
  • prospernnadi3
    2 months ago+ 0 comments

    const modifyArray = (nums) => { return nums.reduce((acc, curr)=> { curr % 2 === 0 ? acc.push(curr * 2) : acc.push(curr * 3); return acc; }, [])

    }

    -1|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy