-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlineUp.js
More file actions
22 lines (17 loc) · 1.08 KB
/
Copy pathlineUp.js
File metadata and controls
22 lines (17 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// To prepare his students for an upcoming game, the sports coach decides to try some new training drills. To begin with, he lines them up and starts with the following warm-up exercise: when the coach says 'L', he instructs the students to turn to the left. Alternatively, when he says 'R', they should turn to the right. Finally, when the coach says 'A', the students should turn around.
// Unfortunately some students (not all of them, but at least one) can't tell left from right, meaning they always turn right when they hear 'L' and left when they hear 'R'. The coach wants to know how many times the students end up facing the same direction.
// Given the list of commands the coach has given, count the number of such commands after which the students will be facing the same direction.
function lineUp(commands) {
var a = commands.toUpperCase().split(''), lr = true, res = 0;
for(var i in a){
if (a[i] == 'L' || a[i] == 'R') {
if(!lr){
res++
}
lr = !lr;
} else {
if(lr) res++;
}
}
return res;
}