题解 | #坐标移动#

坐标移动

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))



全部评论

相关推荐

Yki_:你要算时间成本呀,研究生两三年,博士三四年,加起来就五六年了,如果你本科去腾讯干五年,多领五年的年薪,加上公司内涨薪,可能到时候十五年总薪资也跟博士差不多
点赞 评论 收藏
分享
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-27 18:00
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务