input2
unknown
javascript
4 years ago
718 B
7
Indexable
let input2 = `forward 2
down 9
up 6
forward 1
down 5
down 7`
.split(/\n/)
.map((command) => {
let [direction, distance] = command.split(" ");
return { direction, distance: parseInt(distance) };
});
let horizontalPosition = 0;
let depthPosition = 0;
let result;
function coordinates() {
for (let command of input2) {
switch (command.direction) {
case "forward":
horizontalPosition += command.distance;
break;
case "up":
depthPosition -= command.distance;
break;
case "down":
depthPosition += command.distance;
break;
}
}
return { depth: horizontalPosition, horizontal: depthPosition };
}
console.log(coordinates(input2));
Editor is loading...