-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaxiGeometryOpa.js
More file actions
74 lines (73 loc) · 2.49 KB
/
TaxiGeometryOpa.js
File metadata and controls
74 lines (73 loc) · 2.49 KB
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
const blocksAway = function(directions) {
let x = 0; // <<<< Initialize x
let y = 0; // <<<< Initialize y
let previousStep = '';
let newStep = '';
let rightWay = '';
let leftWay = '';
let i = 0; // <<<< Initialize i
let output = {east: 0, north: 0};
// first step
if ( directions[0] === 'right') {
newStep = 'right'; // <<<< typo mistake
rightWay = 'east';
x = x + directions[i + 1];
} else if ( directions[0] === 'left') {
newStep = 'right';
leftWay = 'north';
y = y + directions[i + 1];
}
console.log("first step: " + newStep + " " + rightWay + " " + leftWay);
console.log("position: " + x + " " + y);
// further steps
for (i = 2 ; i < directions.length ; i += 2) {
if (newStep === 'right' && rightWay === 'east') {
previousStep = 'east';
x = x + directions[i + 1];
} else if (newStep === 'right' && rightWay === 'west') {
previousStep = 'west';
x = x - directions[i - 1];
} else if (newStep === 'right' && rightWay === 'north') {
previousStep = 'north';
y = y + directions[i + 1];
} else if (newStep === 'right' && rightWay === 'south') {
previousStep = 'south';
y = y - directions[i - 1];
}
if (newStep === 'left' && leftWay === 'east') {
previousStep = 'east';
x = x + directions[i + 1];
} else if (newStep === 'left' && leftWay === 'west') {
previousStep = 'west';
x = x - directions[i - 1];
} else if (newStep === 'left' && leftWay === 'north') {
previousStep = 'north';
y = y + directions[i + 1];
} else if (newStep === 'left' && leftWay === 'south') {
previousStep = 'south';
y = y - directions[i - 1];
}
if (previousStep === 'north') {
rightWay = 'east';
leftWay = 'west';
} else if (previousStep === 'south') {
rightWay = 'west';
leftWay = 'east';
} else if (previousStep === 'west') {
rightWay = 'north';
leftWay = 'south';
} else if (previousStep = 'east') {
rightWay = 'south';
leftWay = 'north';
}
console.log("second step: " + newStep + " " + rightWay + " " + leftWay);
console.log("position: " + x + " " + y);
}
console.log("final",x, y);
output.east = x;
output.north = y;
return output;
}
console.log(blocksAway(["right", 2, "left", 3, "left", 1]));
console.log(blocksAway(["left", 1, "right", 1, "left", 1, "right", 1, "left", 1, "right", 1]));
console.log(blocksAway(["left", 3, "right", 1, "right", 3, "right", 1]));