Robot Return to Origin

LeetCode題目: 657. Robot Return to Origin

My solution:

/**
 * @param {string} moves
 * @return {boolean}
 */
let judgeCircle = moves => {
    let originPostion = [0, 0];
    const moveDict = {
        'R': 1,
        'L': -1,
        'U': 1,
        'D': -1,
    }
    for(let i=0; i<moves.length; i++) {
        if(moves[i] === 'R' || moves[i] === 'L') originPostion[0] += moveDict[moves[i]];
        else if(moves[i] === 'U' || moves[i] === 'D') originPostion[1] += moveDict[moves[i]];
    }
    const [x, y] = originPostion;
    return (x === 0 && y === 0);
};
profile-image
Hi, 我是 Zeki。目前為一名前端工程師。我相信科技始終來自於人性,是用來幫助人們過上更有品質的生活的,但願也希望如此。