Day 5: Template Literals

Day 5: Template Literals

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.


Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
* 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) {
const [a, p] = expressions;
const value = Math.sqrt((p ** 2 - (16 * a)));

return [((p - value) / 4), ((p + value) / 4)];
}

Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×