2

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.

1 Answer 1

3

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++)
Sign up to request clarification or add additional context in comments.

1 Comment

Doh! Thanks makes perfect sense! :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.