Day 5: Template Literals | 10 Days Of JavaScript | HackerRank Solution

Hello coders, today we are going to solve Day 5: Template Literals HackerRank Solution which is a part of 10 Days of JavaScript Series.

Day 5: Template Literals

Contents

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 areaA, and the second value denotes its perimeterP.

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 5: Template Literals

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

Disclaimer: The above Problem (Template Literals) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published.