Day 5: Arrow Functions | 10 Days of JavaScript – Hacker Rank Solution

Day 5: Arrow Functions | 10 Days of JavaScript – Hacker Rank Solution: Hey Guy’s In this post we are solving Day 5: Arrow Functions which is a part of 10 Days of JavaScript Series.

Day 5: Arrow Functions | 10 Days of JavaScript – Hacker Rank Solution

Objective

In this challenge, we practice using arrow functions. Check the attached tutorial for more details.

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.

Input Format

The first line contains an integer, n, denoting the size of nums.
The second line contains n space-separated integers describing the respective elements of nums.

Constraints

  • 1 <= n <= 10
  • 1 <= numsi <= 100, where numsi is the ith element of nums.

Output Format

Return the modified array where every even element is doubled and every odd element is tripled.

Sample Input 0

5

1 2 3 4 5

Sample Output 0

3 4 9 8 15

Explanation 0

Given nums = [1, 2, 3, 4, 5], we modify each element so that all even elements are multiplied by 2 and all odd elements are multipled by [1, 2, 3, 4, 5] => [1 x 3, 2 x 2, 3 x 3, 4 x 2, 5 x 3] => [3, 4, 9, 8, 15]. In other words, . We then return the modified array as our answer.

Day 5: Arrow Functions | 10 Days of JavaScript – Hacker Rank Solution

'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });
    main();
});
function readLine() {
    return inputString[currentLine++];
}
/*
 * 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 something = function(n){
        if(n%2==0)
            return n*2;
        else
            return n*3;
    }
    var newArray = nums.map(something);
    return newArray;
}
function main() {
    const n = +(readLine());
    const a = readLine().split(' ').map(Number);
    console.log(modifyArray(a).toString().split(',').join(' '));
}

Disclaimer: The above Problem (Day 5: Arrow Functions) is generated by Hackerrank but the Solution is Provided by Chase2Learn. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the following contact form thank you.

Leave a Comment