JS Goodness – Test if two words are anagrams of each other

Problem Statement : Check if two words are anagram’s of each other
Anagram are words which are made of same characters e.g LOVE and VOLE or Friend & Finder.
Below is the solution for the same



 console.clear();  
 var ref="LOVE"  
 var givenWord="VOLX";  
 console.log("Test if two words are anaggrams of each other")  
 function testAnagram(ref,givenWord){  
  if(ref.length !== givenWord.length)  
    throw new Error("not a valid")  
  var isAnagram = true  
  var refArr = ref.split("").sort()   
  var givenWord = givenWord.split("").sort()  
  var isAnagram = true  
  var result = givenWord.some(function(elem,idx){  
   if(elem === refArr[idx])  
    return false  
   else   
    return true     
  })  
  if(result) isAnagram = false  
  console.log("isAnagram->"+isAnagram)  
 }  
 testAnagram(ref,givenWord);  

Comments

Popular posts from this blog

Apache Airflow Wait Between Tasks

Groovy GoodNess – Converting a List to String

Java Spring Interview Questions