Reputation: 173
So I was messing around with a simple javascript anagram function to compare 2 strings, however whenever I tried to use the .split operation in my sort function my code would error:
var wd;
function sortword(word){
wd = word;
var w = wd.split("");
w.sort();
return w;
}
caused
"TypeError: undefined is not an object (evaluating 'wd.split')"
http://jsbin.com/lebiwolive/1/edit?js,console
Why does this cause such an error? I've tried defining wd in various places but it does;t seem to make any difference. The code does even work correctly but I have this error in my console.
Upvotes: 2
Views: 5342
Reputation: 2404
Check your for loop:
for (i=0; first_words.length; i++)
You didn't put any ending condition, so the loop keeps running after you've read the whole array.
Write this instead:
for (i=0; i<first_words.length; i++)
Upvotes: 3