DEV Community

Madalina Pastiu
Madalina Pastiu

Posted on

Anagram Detection

Instructions:

An anagram is the result of rearranging the letters of a word to produce a new word (see wikipedia).

Note: anagrams are case insensitive

Complete the function to return true if the two arguments given are anagrams of each other; return false otherwise.

Examples
"foefet" is an anagram of "toffee"

"Buckethead" is an anagram of "DeathCubeK"

Solution:

var isAnagram = function(test, original) {
  const ordered = str => str.toLowerCase().split('').sort().join();
  return ordered(test) === ordered(original);
};
Enter fullscreen mode Exit fullscreen mode

Thoughts:

  1. I created a function named ordered that takes a string and transforms it 1st to lower case, then will split to an array of letters and sort it alphabetically. At the ens it does join the string back together in the new order.
  2. I do compare through the function above the 1st string to the 2nd string and returned true or false.

This is a CodeWars Challenge of 7kyu Rank

Top comments (0)