题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
const arr = readline().split(";")
let reg = /^[ASWD]\d{1,2}$/
let newArr = arr.filter(item=> reg.test(item))
let res = [0,0]
newArr.forEach(item=>{
let moveP = item.slice(0,1)
switch(moveP){
case 'A':
res[0] = res[0]-item.slice(1)*1
break;
case 'D':
res[0] = res[0]+item.slice(1)*1
break;
case 'W':
res[1] = res[1]+item.slice(1)*1
break;
case 'S':
res[1] = res[1]-item.slice(1)*1
break;
}
})
console.log(res.join(','))
let reg = /^[ASWD]\d{1,2}$/
let newArr = arr.filter(item=> reg.test(item))
let res = [0,0]
newArr.forEach(item=>{
let moveP = item.slice(0,1)
switch(moveP){
case 'A':
res[0] = res[0]-item.slice(1)*1
break;
case 'D':
res[0] = res[0]+item.slice(1)*1
break;
case 'W':
res[1] = res[1]+item.slice(1)*1
break;
case 'S':
res[1] = res[1]-item.slice(1)*1
break;
}
})
console.log(res.join(','))