E
E
Extazzi2020-04-23 06:02:53
JavaScript
Extazzi, 2020-04-23 06:02:53

I don't fully understand how the code works?

Hello everyone. I don't fully understand how this code works. Explain, please. Task from Codewars:

function dirReduc(plan) {
  var opposite = {
    'NORTH': 'SOUTH', 'EAST': 'WEST', 'SOUTH': 'NORTH', 'WEST': 'EAST'};
  return plan.reduce(function(dirs, dir){
      if (dirs[dirs.length - 1] === opposite[dir])
        dirs.pop();
      else
        dirs.push(dir);
      return dirs;
    }, []);
}
dirReduc(["NORTH", "SOUTH", "SOUTH", "EAST", "WEST", "NORTH", "WEST"]);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Ushenin, 2020-04-23
@usheninmike

All neighboring elements denoting opposite directions are removed from the array.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question