-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_insect.js
74 lines (67 loc) · 1.93 KB
/
my_insect.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
function my_insect()
{
var insectPos = [3,3];
var insectDirection = "E";
var Commands = "FFRFFRFRRF"
for(let i=0;i<Commands.length;i++)
{
if(insectDirection === "N" && Commands.charAt(i) === "L" )
{
insectDirection = "W",
insectPos[0]--
}
else if(insectDirection === "N" && Commands.charAt(i) === "F" )
{
insectPos[1]++
}
else if(insectDirection === "N" && Commands.charAt(i) === "R" )
{
insectDirection = "E",
insectPos[0]++
}
else if(insectDirection === "S" && Commands.charAt(i) === "L" )
{
insectDirection = "E",
insectPos[0]++
}
else if(insectDirection === "S" && Commands.charAt(i) === "F" )
{
insectPos[1]--
}
else if(insectDirection === "S" && Commands.charAt(i) === "R" )
{
insectDirection = "W",
insectPos[0]--
}
else if(insectDirection === "E" && Commands.charAt(i) === "L" )
{
insectDirection = "N",
insectPos[1]++
}
else if(insectDirection === "E" && Commands.charAt(i) === "F" )
{
insectPos[0]++
}
else if(insectDirection === "E" && Commands.charAt(i) === "R" )
{
insectDirection = "S",
insectPos[1]--
}
else if(insectDirection === "W" && Commands.charAt(i) === "L" )
{
insectDirection = "S",
insectPos[1]--
}
else if(insectDirection === "W" && Commands.charAt(i) === "F" )
{
insectPos[0]--
}
else if(insectDirection === "W" && Commands.charAt(i) === "R" )
{
insectDirection = "N",
insectPos[1]++
}
}
return "" + insectDirection +" " + insectPos[0] + " " + insectPos[1];
}
console.log(my_insect())