-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobot Simulator.js
More file actions
33 lines (32 loc) · 801 Bytes
/
Copy pathRobot Simulator.js
File metadata and controls
33 lines (32 loc) · 801 Bytes
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
/**/
var directions=["west","north","east","south"];
var coords=[0,0];
var currDir=1;
var direction=directions[currDir];
var instructions=prompt("Enter the instructions.").split("");
for(var x=0;x<instructions.length;x++){
console.log("The robot is at "+coords[0]+", "+coords[1]+" and is facing "+direction);
if(instructions[x]=="R"){
currDir+=1;
if(currDir==4){
currDir=0;
}
direction=directions[currDir];
}else if(instructions[x]=="L"){
currDir-=1;
if(currDir==-1){
currDir=3;
}
direction=directions[currDir];
}else if(instructions[x]=="A"){
if(currDir==0){
coords[0]-=1;
}else if(currDir==1){
coords[1]+=1;
}else if(currDir==2){
coords[0]+=1;
}else if(currDir==3){
coords[1]-=1;
}
}
}