Codewars | Directions Reduction | JavaScript Solution
This kata got you struggling too huh? Don't stress I got you.
For good measure, if you're just passing by and have no idea what this is, you can find the kata instructions on Codewars.
My solution.
I broke it down into these steps:
- loop through the values of the array
- identify invalid directions {North next to South} and {East next to west West}
- remove invalid directions from the array
- if there still exists invalid directions after completing the loop restart the loop. Else terminate the loop
I struggled for a while trying to figure out how to restart the loop. Eventually, I settled on a while loop. Where the while loop's control statement is a function that checks for the presence of invalid directions and returns a boolean.
like so:
function dirReduc(arr) {
// function to check for the presence of invalid directions
function hasInvalidDirections(array) {
count = 0;
for (let i = 0; i < array.length - 1; i++) {
if (
(array[i] === "NORTH" && array[i + 1] === "SOUTH") ||
(array[i] === "SOUTH" && array[i + 1] === "NORTH") ||
(array[i] === "EAST" && array[i + 1] === "WEST") ||
(array[i] === "WEST" && array[i + 1] === "EAST")
) {
count++;
}
}
return count > 0;
}
// while loop to restart the the nested for-loop
while (hasInvalidDirections(arr)) {
for (let i = 0; i < arr.length; i++) {
if (
(arr[i] === "NORTH" && arr[i + 1] === "SOUTH") ||
(arr[i] === "SOUTH" && arr[i + 1] === "NORTH") ||
(arr[i] === "EAST" && arr[i + 1] === "WEST") ||
(arr[i] === "WEST" && arr[i + 1] === "EAST")
) {
arr.splice(i, 2);
}
}
}
return arr;
}
Other Solutions
I found these on the internet and thought they were interesting. So I'm leaving them here. When I cover the necessary topics I might come back to them. Hopefully, I'll be able to grok them then.
One by Andrew Losseff.
And this one that was top voted in the kata solutions board:
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;
}, []);
}
This freecodecamp forum discussion explains the use of a stack to solve the kata.
And that's it for today folks. Happy coding!