10 Days Of JavaScript

Day 5: Template Literals Solution

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


Also visit this link:ย  Day 5: Inheritance Solution


 

Day 5: Template Literals Solution

Objective

In this challenge, we practice using JavaScript Template Literals.

Task

The code in the editor has a tagged template literal that passes the area and perimeter of a rectangle to a tag function namedย sides. Recall that the first argument of a tag function is an array of string literals from the template, and the subsequent values are the templateโ€™s respective expression values.

Complete the function in the editor so that it does the following:

s = P +- (P^2 โ€“ 16*4)^1/2 /4

  1. Finds the initial values ofย s1ย andย s2ย by plugging theย areaย andย perimeterย values into the formula:whereย Aย is the rectangleโ€™s area andย Pย is its perimeter.
  2. Creates an array consisting ofย s1ย andย s2ย and sorts it in ascending order.
  3. Returns the sorted array.

Input Format

The first line contains an integer denotingย s1.
The second line contains an integer denotingย s2.

Constraints

  • 1 <=ย s1,ย s2ย <= 100

Output Format

Return an array consisting ofย s1ย andย s2, sorted in ascending order.

Sample Input 0

10
14

Sample Output 0

10
14

Explanation 0

The locked code in the editor passes the following arrays to the tag function:

  • The value ofย literalsย isย [ ‘The area is: ‘, ‘.\nThe perimeter is: ‘, ‘.’ ].
  • The value ofย expressionsย isย [ 140, 48 ], where the first value denotes the rectangleโ€™sย area,ย A, and the second value denotes itsย perimeter,ย P.

When we plug those values into our formula, we get the following:

s1ย = 14
s2ย = 10

We then store these values in an array,ย [14, 10], sort the array, and return the sorted array,ย [10, 14], as our answer.

 

Solution โ€“ Day 4: Classes


'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++];
}

/*
ย *ย Determineย theย originalย sideย lengthsย andย returnย anย array:
ย *ย -ย Theย firstย elementย isย theย lengthย ofย theย shorterย side
ย *ย -ย Theย secondย elementย isย theย lengthย ofย theย longerย side
ย *ย 
ย *ย Parameter(s):
ย *ย literals:ย Theย taggedย templateย literal'sย arrayย ofย strings.
ย *ย expressions:ย Theย taggedย templateย literal'sย arrayย ofย expressionย valuesย (i.e.,ย [area,ย perimeter]).
ย */
functionย sides(literals,ย ...expressions)ย {
ย ย ย ย varย areaย =ย expressions[0];
ย ย ย ย varย perimeterย =ย expressions[1];
ย ย ย ย 
ย ย ย ย varย s1ย =ย (perimeterย +ย Math.sqrt(perimeterย *ย perimeterย -ย 16ย *ย area))ย /ย 4;
ย ย ย ย varย s2ย =ย (perimeterย -ย Math.sqrt(perimeterย *ย perimeterย -ย 16ย *ย area))ย /ย 4;
ย ย ย ย varย arrย =ย [s1,ย s2];
ย ย ย ย varย arrSortย =ย arr.sort(functionย (a,ย b)ย {
ย ย ย ย ย ย ย ย returnย aย -ย b;
ย ย ย ย });
ย ย ย ย returnย arrSort;
}

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: