Template literals || JS Hackerrank Solution

 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:

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

where is the rectangle's area and is its perimeter.

2. Creates an array consisting of and and sorts it in ascending order.

3. Returns the sorted array.

Input Format

The first line contains an integer denoting .

The second line contains an integer denoting .

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

  }

 

  function sides(literals, ...expressions) {

   var [a,p]=expressions;

   var s1=(p+Math.sqrt((p*p)-(16*a)))/4;

   var s2=(p-Math.sqrt((p*p)-(16*a)))/4;

   var arr=[];

   arr.push(s1);

   arr.push(s2);

   return arr.sort();

    

 }

 

  function main() {

    let s1 = +(readLine());

    let s2 = +(readLine());

 

 

No comments:

Post a Comment