Take a Ten Minute Walk: A City Tour Solution
var directions = {
  'n': 0,
  's': 0,
  'e': 0,
  'w': 0
}


Quotes are optional on all valid property names.

walk.forEach( function(direction) {
  directions[direction]++;
}


Syntax error here. Plus, making whitespaces consistent would make this code less of an eye sore. Consider using an IDE that will yell at you for inconsistent formatting.

var displacement = {
  x: directions['n'] - directions['s'],
  y: directions['e'] - directions['w']
}


Storing in an object is unnecessary. x and y can just be variables here.



An alternate approach is to represent the directions into 2 columns, vertical and horizontal. Then the directions on the same axis are represented 1 and -1. This way, knowing if you're back is a matter of checking if both axes sum up to zero. No subtraction required.

   V  H
N  1  0
S -1  0 
E  0  1
E  0  1
W  0 -1
Result: [0, 1] 
// Off by 1 to the east. Also, length is less than 10.


This is also a good functional programming exercise on how to think of functions as data transformations instead of machine commands. In the following code, each array method transforms the data from directions to a boolean in three discrete steps.



const directionVector = {
  N: [1, 0],
  S: [-1, 0],
  E: [0, 1],
  W: [0, -1]
}

const isValidWalk = walk =&gt; walk.length === 10 &amp;&amp; walk
  .map(direction =&gt; directionVector[direction])
  .reduce((prev, next) =&gt; [prev[0] + next[0], prev[1] + next[1]], [0, 0])
  .every(v =&gt; v === 0)

// length check goes first so code doesn't even try if it's not 10
// map converts direction into vectors: N -&gt; [V, H]
// reduce sums all vectors: [V1, H1] + [V2, H2] + ... = [Vs, Hs]
// every checks if sum values are all zero: Vs === 0, Hs === 0

// Too short
console.log(isValidWalk(['N', 'S', 'E', 'W', 'W', 'E']))

// Too long
console.log(isValidWalk(['N', 'S', 'E', 'W', 'N', 'S', 'E', 'W', 'N', 'S', 'E']))

// We're back!
console.log(isValidWalk(['N', 'S', 'E', 'W', 'N', 'S', 'E', 'W', 'N', 'S']))

// We're lost
console.log(isValidWalk(['N', 'S', 'E', 'W', 'N', 'S', 'E', 'W', 'N', 'E']))