In this HackerRank Day 5: Template Literals 10 days of javascript problem solution 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.
  2. where A is the rectangle's area and P is its perimeter.
  3. Creates an array consisting of s1 and s2 and sorts it in ascending order.
  4. Returns the sorted array.

HackerRank Day 5: Template Literals problem solution


Problem solution in JavaScript programming.

/*
 * 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) {
    let exp0 = expressions[0];
    let exp1 = expressions[1];
    
    console.log((exp1 - Math.sqrt(exp1 * exp1 - (16 * exp0))) / 4);
    console.log((exp1 + Math.sqrt(exp1 * exp1 - (16 * exp0))) / 4);
}