mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-14 23:12:58 +08:00
Create anagram.js
two strings are anagram or not
This commit is contained in:
parent
b9d0d9ab38
commit
04b5a93fdf
24
src/algorithms/string/anagram.js
Normal file
24
src/algorithms/string/anagram.js
Normal file
@ -0,0 +1,24 @@
|
||||
// TO CHECK IF TWO STRINGS ARE ANAGRAM OR NOT
|
||||
|
||||
function anagram(word1, word2){
|
||||
var sorted1 = word1.split("").sort().join("");//SORTING WORD1
|
||||
var sorted2 = word2.split("").sort().join("");//SORTING WORD2
|
||||
|
||||
//COMPARING LENGTH AND CHECKING ===
|
||||
|
||||
if (sorted1.length == sorted2.length && sorted1 === sorted2) {
|
||||
return true;
|
||||
}
|
||||
|
||||
else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if(anagram(word1,word2)){
|
||||
alert("AN ANAGRAM");
|
||||
}
|
||||
else{
|
||||
alert("NOT AN ANAGRAM");
|
||||
}
|
||||
anagram("sasdas", "sdasda");
|
Loading…
Reference in New Issue
Block a user