'TypeError: function is not a function
Doing some jasmine testing with JS. So I have the testing function which seems reasonable(only running the first test currently)
describe('Anagram', function() {
it('no matches',function() {
var subject = new Anagram('diaper');
var matches = subject.matches([ 'hello', 'world', 'zombies', 'pants']);
expect(matches).toEqual([]);
});
Then I have my simple function
var Anagram = function(string){
this.word = string;
};
Anagram.prototype.matches = function(array){
var answer = [];
var splitWord = this.word.split('').sort();
for(var i = 0; i < array.length; i++){
var isAnagram = true;
var splitItem = array[i].split('').sort();
for(var j = 0; j < splitWord.length; j++){
if(splitWord[j] !== splitItem[j]){
isAnagram = false;
}
}
if(isAnagram === true){
answer.push(array[i]);
}
}
return answer;
};
module.export = Anagram;
My function is supposed to take a string and then look at an array of strings and return the anagram strings. I keep getting TypeError: Anagram is not a function. Tried looking for answers and most of it seemed to relate to semi colons which I think are not an issue since I use one after variable declaration and method declaration. I'd really just like to know what that typeError means if it's a common one to know or what the most likely reasons to get it are.
Solution 1:[1]
To check whether two strings are anagram or not, one can use this function. If the strings are anagram, then it will return true.
function isAnagram(str1,str2){
// str to alphabet array
let arr1 = Array.from(str1);
let arr2 = Array.from(str2);
//checking length
let n1 = arr1.length;
let n2 = arr2.length;
if (n1 !== n2){
return false;
}
//sorting array
arr1.sort();
arr2.sort();
for (let i=0; i<n1; i++ ){
if ( arr1[i] !== arr2[i]){
return false;
}
}
return true;
}
// Example: checking "listen and silent
console.log(isAnagram("listen", "silent"));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sreedevi |
