-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjumpingOnClouds.js
38 lines (34 loc) · 898 Bytes
/
jumpingOnClouds.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
'use strict'
const c1 = [0, 0, 1, 0, 0, 1, 0]; //4
const c2 = [0, 0, 0, 0, 1, 0]; //3
const c3 = [0, 0, 1, 0, 0, 0, 0, 1, 0, 0]; //6
const c4 = [0, 1, 0]; //1
const c5 = [0, 0] //1
function jumpingOnClouds(c) {
let steps = 0;
if (c[c.length - 3] === 1) {
steps++;
}
if (c.length === 2) {
steps++;
}
for (let i = 0; i < c.length; i++) {
if (c[i] === 0 && c[i + 1] === 0 && c[i + 2] === 0 ||
c[i] === 0 && c[i + 1] === 1 && c[i + 2] === 0) {
steps++;
c.splice(0, 2);
} else if (c[i] === 0 && c[i + 1] === 0 && c[i + 2] === 1) {
steps++;
c.splice(0, 1);
} else if (c[i] === 0) {
c.splice(0, 1);
}
i--;
}
return steps;
};
console.log(jumpingOnClouds(c1), '4');
console.log(jumpingOnClouds(c2), '3');
console.log(jumpingOnClouds(c3), '6');
console.log(jumpingOnClouds(c4), '1');
console.log(jumpingOnClouds(c5), '1');