Reputation: 221
Just got back my test for my Javascript class and I missed a question that I felt quite certain about.
We were supposed to make a function that returned true if two strings were anagrams, and false otherwise. I cant find an instance where this wouldn't work. I would appreciate any help!
This is what I had:
function isAnagram(str1, str2){
if(str1.length !== str2.length){
return false;
}
else{
for(var i = 0; i < str1.length; i++){
if(!str2.includes(str1[i])){
return false;
}
}
return true;
}
}
Upvotes: 1
Views: 153
Reputation: 347
Simply compare both the strings character by character and register how many matches you find. The total matches must be >= to the string length (to cater to the cases where the characters might be repeating e.g. boot)
var i, j;
var z = 0;
function checkAnagram(str1, str2) {
if (str1.length != str2.length) { return `${str1} and ${str2} are not anagram, lengths dont match!`; }
else {
for (i = 0; i < str1.length; i++) {
for (j = 0; j < str2.length; j++) {
if(str1[i] == str2[j]) {
// console.log('matches',str1[i],str2[j]);
z++;
}
}
}
// console.log(z);
if (z>=str1.length)
return`${str1} and ${str2} are anagram!`;
else return `${str1} and ${str2} are not anagram!`;
}
}
console.log(checkAnagram('fired','fried'));
console.log(checkAnagram('logo','ogol'));
console.log(checkAnagram('abcd','abc'));
Upvotes: 0
Reputation: 2115
Single characters can occur multiple times in your function, for example comparing test
and tttt
returns true.
One way to modify your function is to remove the character from str2
each time one is found in the for loop:
function isAnagram(str1, str2){
if(str1.length !== str2.length) return false;
for(var i = 0; i < str1.length; i++){
var found = str2.indexOf(str1[i]);
if(found === -1) return false;
str2 = str2.substr(0,found) + str2.substr(found + 1)
}
return true;
}
Upvotes: 1
Reputation: 9246
It only checks that each letter of str1
is present in str2
, regardless of how many times they appear.
EDIT: It also doesn't check if str2
has letters that don't appear in str1
.
function isAnagram(str1, str2) {
if (str1.length !== str2.length) {
return false;
} else {
for (var i = 0; i < str1.length; i++) {
if (!str2.includes(str1[i])) {
return false;
}
}
return true;
}
}
console.log(isAnagram('hello', 'hlleo'), ', should be true'); // true: correct
console.log(isAnagram('bye', 'byee'), ', should be false'); // false: correct
console.log(isAnagram('byeb', 'byee'), ', should be false'); // true: incorrect
console.log(isAnagram('byeb', 'byte'), ', should be false'); // true: incorrect
Upvotes: 2
Reputation: 41893
Alternative solution.
const isAnagram = (s1, s2) => {
return lowerify(s1) == lowerify(s2);
}
const lowerify = str => str.toLowerCase().split('').sort().join('');
console.log(isAnagram("Arrigo Boito", "Tobia Gorrio"));
console.log(isAnagram("abc", "CBa"));
console.log(isAnagram("deep", "depp"));
Upvotes: 3
Reputation: 8653
as a test case, your code would return success on this, but violate the rules of being an anagram :
isAnagram("ooaooa", "oooooa");
it just checks if the same amount of letters are present, and if every letter from word a is present in word b, but not the count of the letters in both words.
Upvotes: 1
Reputation: 943608
It can't handle the case where you have two words, of equal length, using the same letters, but with different numbers of each letter.
This gives a false positive:
console.log(isAnagram("deep", "depp"))
Upvotes: 2
Reputation: 14875
Because your function would, for example, fail this test:
isAnagram('abba', 'abbb') === false
Upvotes: 1