Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MinSomai/68ac4d913e1d7fe34e622fbdf81d4145 to your computer and use it in GitHub Desktop.
Save MinSomai/68ac4d913e1d7fe34e622fbdf81d4145 to your computer and use it in GitHub Desktop.
Day 5 : Javascript | Template Literals - Hackerrank.js
'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) {
const [area, perimeter] = expressions;
let s1 = (perimeter + Math.sqrt((perimeter*perimeter) - (16 * area)))/4;
let s2 = (perimeter - Math.sqrt((perimeter*perimeter) - (16 * area)))/4;
return [s1, s2].sort()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment