题解 | #坐标移动#
坐标移动
http://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
const line = readline();
function setData(line) {
let arr = line.split(';');
let point = new Array(2).fill(0);
//过滤非法字符
arr = arr.map(x=> {
if(/^[A-Z]\d{1,2}$/g.test(x)){
return x;
}else {
return '';
}
});
for(let i=0; i<arr.length; i++) {
if(!arr[i]) {
continue;
}
let str = arr[i].substr(0,1);
let num = parseInt(arr[i].substr(1,arr[i].length));
if(arr[i].indexOf('A') !== -1) {// 向左
point[0] -= num;
}
if(arr[i].indexOf('D') !== -1) {// 向右
point[0] += num;
}
if(arr[i].indexOf('W') !== -1) {// 向上
point[1] += num;
}
if(arr[i].indexOf('S') !== -1) {// 向下
point[1] -= num;
}
}
console.log(point.join(','));
}
setData(line);
function setData(line) {
let arr = line.split(';');
let point = new Array(2).fill(0);
//过滤非法字符
arr = arr.map(x=> {
if(/^[A-Z]\d{1,2}$/g.test(x)){
return x;
}else {
return '';
}
});
for(let i=0; i<arr.length; i++) {
if(!arr[i]) {
continue;
}
let str = arr[i].substr(0,1);
let num = parseInt(arr[i].substr(1,arr[i].length));
if(arr[i].indexOf('A') !== -1) {// 向左
point[0] -= num;
}
if(arr[i].indexOf('D') !== -1) {// 向右
point[0] += num;
}
if(arr[i].indexOf('W') !== -1) {// 向上
point[1] += num;
}
if(arr[i].indexOf('S') !== -1) {// 向下
point[1] -= num;
}
}
console.log(point.join(','));
}
setData(line);