-
Notifications
You must be signed in to change notification settings - Fork 0
/
isAnagram.js
34 lines (27 loc) · 876 Bytes
/
isAnagram.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
Anagrams are groups of words that can be spelled with the same letters.
For example, the letters in "pea" can be rearrange to spell "ape", and
the letters in "allergy" can be rearranged to spell "gallery."
Write a function to check if two strings of lowercase letters are anagrams.
Return true if the word is an anagram. Return false if it isn't.
Example input: "allergy", "gallery"
Example output: true
Example input: "rainbow", "crossbow"
Example output: false
*/
let str1 = "angel";
let str2 = "glean";
function isAnagram(str1, str2) {
const sortedStr1 = str1.split('').sort().join('');
const sortedStr2 = str2.split('').sort().join('');
if (str1.length !== str2.length) {
return false;
}
else if (sortedStr1 === sortedStr2) {
return true;
}
else {
return false;
}
}
console.log(isAnagram(str1, str2));