题解 | #坐标移动#
坐标移动
http://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
const line = readline();
const moveActionList = {
A: (x, y, number) => {
x -= number;
return [x, y];
},
D: (x, y, number) => {
x += number;
return [x, y];
},
W: (x, y, number) => {
y += number;
return [x, y];
},
S: (x, y, number) => {
y -= number;
return [x, y];
},
};
const computedCoordinate = (str) => {
let [x, y] = [0, 0];
const list = str.split(";");
list.map((item) => {
const wordFlag = item.substring(0, 1);
const numberFlag = item.substring(1, item.length);
if (moveActionList[wordFlag] && /^\d{1,}$/.test(numberFlag)) {
[x,y] = moveActionList[wordFlag](x, y, Number(numberFlag))
}
});
return `${x},${y}`;
};
console.log(computedCoordinate(line))
