Skip to content

Commit 1e5f130

Browse files
authored
Create 1138-alphabet-board-path.js
1 parent 103a26f commit 1e5f130

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

1138-alphabet-board-path.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* @param {string} target
3+
* @return {string}
4+
*/
5+
const alphabetBoardPath = function(target) {
6+
let sx = 0,
7+
sy = 0;
8+
let dir = "";
9+
let a = "a".charCodeAt(0);
10+
for (let c of target) {
11+
let dx = (c.charCodeAt(0) - a) % 5,
12+
dy = ((c.charCodeAt(0) - a) / 5) >> 0,
13+
n;
14+
if (sx > dx) {
15+
n = sx - dx;
16+
while (n--) dir += "L";
17+
}
18+
if (sy < dy) {
19+
n = dy - sy;
20+
while (n--) dir += "D";
21+
}
22+
if (sy > dy) {
23+
n = sy - dy;
24+
while (n--) dir += "U";
25+
}
26+
if (sx < dx) {
27+
n = dx - sx;
28+
while (n--) dir += "R";
29+
}
30+
dir += "!";
31+
(sx = dx), (sy = dy);
32+
}
33+
return dir;
34+
};

0 commit comments

Comments
 (0)