Algorithm


Hacker rank Problem Name: 10 Days of JavaScript -Day 5: Template Literals

Hacker rank Problem Link: https://www.hackerrank.com/challenges/js10-template-literals/problem?isFullScreen=true

Objective

In this challenge, we practice using JavaScript Template Literals. Check the attached tutorial for more details.

 

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:

  1. Finds the initial values of s1 and s2 by plugging the area and perimeter values into the formula:

$∗∗(s=(P+−sqrt(P−16A))/4)P">

$

  • where A is the rectangle’s area and P is its perimeter.
  1. Creates an array consisting of s1 and s2 and sorts it in ascending order.
  2. 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

1
2
10
14

Sample Output 0

1
2
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:

1
2
s1=
s2=

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

 

 

 

Code Examples

#1 Code Example with Javascript Programming

Code - Javascript Programming


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

function sides(literals, ...expressions) {
    var area = expressions[0];
    var perimeter = expressions[1];
    var s1 = (perimeter + Math.sqrt(perimeter * perimeter  - (16 * area))) / 4;
    //console.log("s1: " + s1);
    var s2 = (perimeter - Math.sqrt(perimeter * perimeter  - (16 * area))) / 4;
    //console.log("s2: " + s2);
    var array = [s1, s2];
    array =  array.sort(function (a,b) {return a-b;});
    return array;

}

Copy The Code & Try With Live Editor

Input

x
+
cmd
10 14

Output

x
+
cmd
10 14
Advertisements

Demonstration


Previous
[Solved] #3 Day 3: Arrays in Javascript HackerRank - HackerRank Javascript in 10 days
Next
[Solved] #6 Day 6: Bitwise Operators in Javascript HackerRank - HackerRank Javascript in 10 days