Write Up Code Wars — Shifter Words Solutions JavaScript

Code Wars is best platform for learning algorithm from programming language, here I want to write the solution for ‘Shifter Words’ challenge using JavaScript. When it was written, the challenge is still in kyu 7.

You probably know that some characters written on a piece of paper, after turning this sheet 180 degrees, can be read, although sometimes in a different way. So, uppercase letters "H", "I", "N", "O", "S", "X", "Z" after rotation are not changed, the letter "M" becomes a "W", and Vice versa, the letter "W" becomes a "M".We will call a word "shifter" if it consists only of letters "H", "I", "N", "O", "S", "X", "Z", "M" and "W". After turning the sheet, this word can also be read, although in a different way. So, the word "WOW "turns into the word "MOM". On the other hand, the word "HOME" is not a shifter.Find the number of unique shifter words in the input string (without duplicates). String contains only uppercase letters.
shifter("SOS IN THE HOME") === 2 // shifter words are "SOS" and "IN"
shifter("WHO IS SHIFTER AND WHO IS NO") === 3 // shifter words are "WHO", "IS", "NO"
shifter("TASK") === 0 // no shifter words
shifter("") === 0 // no shifter words in empty string

As we probably know, that there some character of letters in uppercase when we rotating it, we will see the same character. From the Question, we know that the character are “H”, “I”, “N”, “O”, “S”, “X”, “Z”, “M” and “W”. So in my answer I declare it first as base.

const base = ["H", "I", "N", "O", "S", "X", "Z", "M", "W"];

Next we know from the example, that we will given a string from the function, that sometimes the string will be the same. So I use Set function to split every word and eliminating the same words. So it can make the code more cleaner.

arrays = [...new Set(str.split(" "))];

After that, I set the make counter variable and set it into 0. So when using the loop method it will be added and return as the result. But before that I check the string length. If the string is “” (empty). It will be [] for the arrays like this.

let counter = 0;if (str.length === 0) arrays = [];

In the main function I use forEach function and also using split and every to check the base if every letter of the word is same with the base that I declared at the beginning. If true, the counter value will be increment by 1.

arrays.forEach((word) => {console.log(word);let check = word.split("").every((letter) => base.includes(letter));if (check) counter++;});

Lastly, I return the counter to see the result

return counter;

Here is the source code

Thanks for reading until the end of the article. For the bonus, I will give the answer from another person using REGEX. Trust me, it’s simple but, really great.

Yeah only one line of code, so smart right? It takes Set from the input from the function and using REGEX it filter all shifter character, lastly, it count the size.

I hope you enjoy this article, have a nice code 🎉

A Stoic Web Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store