Codewars - solving an easy JavaScript Kata

One thing that I need to continue to work on (and will probably always have to in order to keep learning and growing) is coding problems. For this, I'm going to head over to codewars.com and work on a code challenge.


For this particular code challenge, the language I'm going to work with is JavaScript. It's not my strongest language so bear with me.


Here is what the kata says:
Write a function that will find all the anagrams of a word from a list. You will be given two inputs a word and an array with words. You should return an array of all the anagrams or an empty array if there are none. For example:


So, my first thought is to convert the words into arrays of individual letters. Then sort them and compare. If the words in the words array match the word, then it works.


To view the Kata, go here



I take the word that's passed in, split it into an array of letters. This could look like this
word = "jen" when split ["j", "e", "n"]. When sorted, it would turn into
["e", "j", "n"]. The filter function will loop through each item in the array, split the word,
sort the letters and then compare the joined version to the word being passed in
after it has been sorted and joined. The idea would be that "ejn" would equal "ejn"
I then return the value of the sorted words that match.

I like to use repl.it a site where you can test out code in the browser to see how my code works before I submit it.
That way I can see if what I think is happening is actually happening.

This code passed all of my tests on the first try. Yay!

Comments

Popular Posts