mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-09-20 07:43:04 +08:00
Merge 5279eb2098
into ca3d16dcce
This commit is contained in:
commit
7377c908d9
21
src/algorithms/string/anagram.js
Normal file
21
src/algorithms/string/anagram.js
Normal file
@ -0,0 +1,21 @@
|
||||
// TO CHECK IF TWO STRINGS ARE ANAGRAM OR NOT
|
||||
|
||||
function anagram(word1, word2) {
|
||||
let sorted1 = word1.split('').sort().join(''); //SORTING WORD1
|
||||
let 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('binary', 'brainy')){
|
||||
alert('AN ANAGRAM');
|
||||
}
|
||||
else{
|
||||
alert('NOT AN ANAGRAM');
|
||||
}
|
Loading…
Reference in New Issue
Block a user