题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
正则表达式去选字符串
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on("line", function (line) {
const tokens = line.split(";");
//遍历 利用正则去匹配合适的数字
let input = [];
let x = 0;
let y = 0;
tokens.forEach((c) => {
if (c) {
let re = /^[ADWS]\d{1,2}$/;
if (c.match(re)) {
//截取字符串
let char = c.substring(0, 1);
let num = parseInt(c.slice(1))
switch (char){
case "A":
x -= num;
break;
case "D":
x += num;
break;
case "W":
y += num;
break;
case "S":
y -= num;
break;
}
}
}
});
console.log(x + "," + y);
});