题解 | #坐标移动#

坐标移动

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



全部评论

相关推荐

牛大宝儿236:还没入职就PUA,[发火我之前遇到一个月给500块钱的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务