Anagram

Write a function that takes two words (string) and returns true or false (boolean) depending on whether or not they are anagrams.

  • An anagram consists of forming a word by rearranging all the letters of another initial word.
  • There is no need to check that both words exist.
  • Two exactly the same words are not an anagram.
solution.js
1function anagram(wordOne, wordTwo) {
2 if (wordOne.length !== wordTwo.length || wordOne === wordTwo) {
3 return false;
4 }
5
6 wordOne = wordOne.toLowerCase();
7 wordTwo = wordTwo.toLowerCase();
8
9 const arrayOne = wordOne.split("").sort();
10 const arrayTwo = wordTwo.split("").sort();
11
12 for (let i = 0; i < arrayOne.length; i++) {
13 if (arrayOne[i] !== arrayTwo[i]) {
14 return false;
15 }
16 }
17
18 return true;
19}
20
21console.log(anagram("rana", "Rana"));
22console.log(anagram("rana", "rana"));
23console.log(anagram("rnaa", "anra"));