-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution0046.js
57 lines (39 loc) · 1.36 KB
/
solution0046.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
---------- 7 Kyu - Highest and Lowest -------------------
Instructions
In this little assignment you are given a string of space separated numbers,
and have to return the highest and lowest number.
Examples
highAndLow("1 2 3 4 5"); // return "5 1"
highAndLow("1 2 -3 4 5"); // return "5 -3"
highAndLow("1 9 3 4 -5"); // return "9 -5"
Notes
All numbers are valid Int32, no need to validate them.
There will always be at least one number in the input string.
Output string must be two numbers separated by a single space, and highest number is first.
---------------
Sample tests
const chai = require("chai");
const assert = chai.assert;
chai.config.truncateThreshold=0;
describe("Example tests", () => {
it("Test 1", () => {
assert.strictEqual(highAndLow("8 3 -5 42 -1 0 0 -9 4 7 4 -4"), "42 -9");
});
it("Test 2", () => {
assert.strictEqual(highAndLow("1 2 3"), "3 1");
});
});
---------------
Psuedo Code
-we have a string with #'s separated by a space, so use .split(' ') to separate them
into single numbers
-we can then use Math.max(...) and Math.min(...) to find highest/lowest
Lessons Learned:
-you can use the spread operator ('...') from ES6 in order to apply Math.max&min to an array
-----------------
*/
function highAndLow(numbers){
let splitNum = numbers.split(' ');
return `${Math.max(...splitNum)} ${Math.min(...splitNum)}`;
}