-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay 11 - AvoidObstacles.js
49 lines (34 loc) · 1.14 KB
/
Day 11 - AvoidObstacles.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
/*Avoid Obstacles
https://scrimba.com/scrim/coabc4ce2aba0f1f0adf8874c
DESCRIPTION:
You are given an array of integers representing coordinates of obstacles situated on a straight line.
Assume that you are jumping from the point with coordinate 0 to the right. Ypu are allowed only to make jumps
of the same length represented by some integer.
Find the minimal length of the jump enough to avoid all the obstacles.
Example:
For inputArray = [5,3,6,7,9], the output should be avoidObstacles(inputArray) = 4
Hints: sort(), every()
*/
const avoidObstacles = (nums) => {
let maxNum = Math.max(...nums);
for (let i=1; i<maxNum; i++) {
if(nums.every((currentNumber) => currentNumber % i !== 0)) {
return i;
}
}
}
/**
* Test Suite
*/
describe('avoidObstacles()', () => {
it('returns minimal number of jumps in between numbers', () => {
// arrange
const nums = [5, 3, 6, 7, 9];
// act
const result = avoidObstacles(nums);
// log
console.log("result: ", result);
// assert
expect(result).toBe(4);
});
});