Answer the question
In order to leave comments, you need to log in
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
All neighboring elements denoting opposite directions are removed from the array.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question