-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06.js
32 lines (26 loc) · 832 Bytes
/
06.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
function maxDistance(movements) {
let distance = 0;
let last_movement = "*";
for (i in movements) {
let current_direction = movements[i];
if (current_direction === ">") {
distance += 1;
last_movement = current_direction;
} else if (current_direction === "<") {
distance -= 1;
last_movement = current_direction;
} else if (current_direction === "*") {
last_movement === "<" ? (distance -= 1) : (distance += 1);
}
}
return Math.abs(distance);
}
const movements = ">>*<";
const result = maxDistance(movements);
console.log(result); // -> 2
const movements2 = "<<<>";
const result2 = maxDistance(movements2);
console.log(result2); // -> 2
const movements3 = ">***>";
const result3 = maxDistance(movements3);
console.log(result3); // -> 5