Day 5: Template Literals | 10 Days of JavaScript – Hacker Rank Solution

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

We also provide Hackerrank solutions in CC++Java programming, and Python Programming languages so whatever your domain we will give you an answer in your field.

You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C ProgrammingHackerRank C++ ProgrammingHackerRank Java Programming, HackerRank Python ProgrammingHackerRank Linux ShellHackerRank SQL Programming, and HackerRank 10 days of Javascript.

Day 5: Template Literals | 10 Days of JavaScript – Hacker Rank Solution

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology,Data StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin Assessment, and Coursera Quiz Answers.

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 + (P2 – 16 X A)1/2 /4

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 <= s1s2 <= 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:

s = P + (P2 – 16 X A)1/2 /4 = (48 + 8) / 4 = 14

s = P – (P2 – 16 X A)1/2 /4 = (48 – 8) / 4 = 10

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



Day 5: Template Literals | 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++];
}
/*
 * 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) {
    // ES6 allows destructuring of arrays into multiple variables
    const [a, p] = expressions;
    // Perform this operation only once and assign to variable
    const root = Math.sqrt((p*p)-(16*a))
    const s1 = (p + root)/4;
    const s2 = (p - root)/4;
    // s2 will always be smaller because of the (-/+) above
    return ([s2, s1]);
}
function main() {
    let s1 = +(readLine());
    let s2 = +(readLine());
    [s1, s2] = [s1, s2].sort();
    const [x, y] = sides`The area is: ${s1 * s2}.\nThe perimeter is: ${2 * (s1 + s2)}.`;
    console.log((s1 === x) ? s1 : -1);
    console.log((s2 === y) ? s2 : -1);
}

Disclaimer: The above Problem (Day 5: Template Literals) 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.

FAQ:

10 days of javascript hackerrank
Answer: Join over 16 million developers in solving code challenges on HackerRank, one of the best ways to prepare for programming interviews. 10 Days of JavaScript

How can I learn JavaScript in 10 days?
Answer: Many free and paid courses and tutorials are available to learn JavaScript. For in-depth knowledge, you can also purchase a few good books like A Smarter Way to Learn JavaScript and Effective JavaScript: 68 Specific Ways to Harness the Power of JavaScript. However, to start working on projects, you need only a little theory and more hands-on, so we recommend you to take up tutorials and short courses that will get you onboard quickly.

Does hacker rank have JavaScript?
Answer: They might not have a specific practice tutorial for it. But you can solve any of their general problems using Javascript.

What is HackerRank JS?
Answer: The HackerRank Skills Certification Test is a standardized assessment to help developers prove their coding skills.

Finally, we are now, in the end, I just want to conclude some important message for you

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Please share our posts on social media platforms and also suggest to your friends to Join Our Groups. Don’t forget to subscribe. 

Sharing Is Caring

Leave a Comment