题解 | #坐标移动#

坐标移动

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



全部评论

相关推荐

05-12 11:09
已编辑
门头沟学院 后端
SmileDog12138:没必要放这么多专业技能的描述。这些应该是默认已会的,写这么多行感觉在凑内容。项目这块感觉再包装包装吧,换个名字,虽然大家的项目基本都是网上套壳的,但是你这也太明显了。放一个业务项目,再放一个技术项目。技术项目,例如中间件的一些扩展和尝试。
点赞 评论 收藏
分享
马上要带我人生中的第一个实习生了,想问问大家都喜欢什么的mentor?好让我有个努力的目标
拒绝996的劳伦斯很勇敢:看得见目标且护犊子的 具体就是明确告诉组员要干什么,然后当别的组甩dirty work时能护的组自家新人
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务