10 Days Of JavaScript

Day 5: Arrow Functions Solution

Hello Friends in this article i am gone to share Hackerrank 10 days of javascript solutions with you. | Day 5: Arrow Functions Solution.


Also visit this link:ย  Day 5: Template Literals Solution


 

Day 5: Arrow Functions Solution

Objective

In this challenge, we practice usingย arrow functions.

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ย 3. In other words,ย [1, 2, 3, 4, 5]ย = [1*3, 2*2, 3*3, 4*2, 5*3] = [3, 4, 9, 8, 15]. We then return the modified array as our answer.

 

Solution โ€“ Day 5: Arrow Functions


'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)ย {
ย ย ย ย constย funcย =ย nums.map(functionย (num){
ย ย ย ย ย ย ย ย ifย (numย %ย 2ย ==ย 0){
ย ย ย ย ย ย ย ย ย ย ย ย returnย 2ย *ย num;
ย ย ย ย ย ย ย ย }ย elseย {
ย ย ย ย ย ย ย ย ย ย ย ย returnย 3ย *ย num;
ย ย ย ย ย ย ย ย }
ย ย ย ย });
ย ย ย ย returnย func;
}


functionย main()ย {
ย ย ย ย constย nย =ย +(readLine());
ย ย ย ย constย aย =ย readLine().split('ย ').map(Number);
ย ย ย ย 
ย ย ย ย console.log(modifyArray(a).toString().split(',').join('ย '));
}

Complete the function in the editor. It has two parameters: a and b. It must return an object modeling a rectangle that has the following properties: Complete the function in the editor. It has two parameters:ย aย andย b. It must return an object modeling a rectangle that has the following properties: