Robot Return to Origin The task
is taken from leetcode


  There is a robot starting at position (0, 0), the origin, on a 2D
  plane. Given a sequence of its moves, judge if this robot ends up at
  (0, 0) after it completes its moves.
  
  The move sequence is represented by a string, and the character
  moves[i] represents its ith move. Valid moves are R (right), L (left),
  U (up), and D (down). If the robot returns to the origin after it
  finishes all of its moves, return true. Otherwise, return false.
  
  Note: The way that the robot is "facing" is irrelevant. "R" will
  always make the robot move to the right once, "L" will always make it
  move left, etc. Also, assume that the magnitude of the robot's
  movement is the same for each move.
  
  Example 1:
  
  Input: "UD" Output: true  Explanation: The robot moves up once, and
  then down once. All moves have the same magnitude, so it ended up at
  the origin where it started. Therefore, we return true.  
  
  Example 2:
  
  Input: "LL" Output: false Explanation: The robot moves left twice. It
  ends up two "moves" to the left of the origin. We return false because
  it is not at the origin at the end of its moves.


const arr = "UD";


My imperative solution:

function judgeCircle(moves) {
  let horizontal = 0;
  let vertical = 0;

  [...moves].forEach(m =&gt; {
    switch(m) {
      case "U":
        ++vertical;
        break;
      case "D":
        --vertical;
        break;
      case "R":
        ++horizontal;
        break;
      case "L":
        --horizontal;
        break;
      default:
        // be a good code
    }
  });
  return !(horizontal) &amp;&amp; !(vertical);
};

console.log(judgeCircle(arr));


My functional solution:

const judgeCircle2 = moves =&gt; {
  const {h, v} = [...moves].reduce((acc,m) =&gt; {
    switch(m) {
      case "U":
        ++acc.v;
        return acc;
      case "D":
        --acc.v;
        return acc;
      case "R":
        ++acc.h;
        return acc;
      case "L":
        --acc.h;
        return acc;
      default:
        // be a good code
    }
  }, {h: 0, v: 0});
  return !h &amp;&amp; !v;
};

console.log(judgeCircle2(arr));
It could be made more functional by avoiding the mutation of acc. The effects are contained within judgeCircle so it's not a big deal, but it feels like if you're going to mutate the accumulator, you might as well just use an imperative loop.

I also preferred to be explicit about the final check. I find the intent of !h &amp;&amp; !v isn't quite as clear as h == 0 &amp;&amp; v == 0;.

I ended up with:

const judgeCircle3 = moves =&gt; {
  const [hori, vert] = [...moves].reduce(([h, v], move) =&gt; {
    switch(move) {
      case "U":
        return [h, v + 1];

      case "D":
        return [h, v - 1];

      case "R":
        return [h + 1, v];

      case "L":
        return [h - 1, v];
    }

  }, [0, 0]);

  return hori == 0 &amp;&amp; vert == 0;
};


The need for the switch here is unfortunate, but the only other alternative I could think of was some mess where a you'd do a lookup on a map which returned a function that returned a "altered" accumulator state.

I also got rid of the default since it didn't seem to be doing anything. You could have done error handling in there (and should in most cases), but if it's a challenge with predefined input, that's probably not necessary unless it's part of the challenge.