0

I have a script that is (seemingly) randomly stopping at the end of a for loop. It completes each iteration, and it completes everything within each iteration, but then it mysteriously stops. As you can see, the "Moving on" alert never fires. ANY help is appreciated!

CODE:

var sliderDirectionRaw = "top-left,left";
var sliderDirection = sliderDirectionRaw.split(",");
    for (i=0;i<=sliderDirection.length;i++) {
        if (sliderDirection[i].indexOf("-") >= 0) {
            sliderDirection[i] = sliderDirection[i].split("-");
                            alert("Multiple directions specified in iteration "+i);
        }
        alert("Direction iteration "+i+" finished");
        if (i == sliderDirection.length-1) {
            alert("Direction loop finished");
        }
    }
alert("Moving on from Direction loop");

JSFiddle: http://jsfiddle.net/k7cSE/1/

6
  • 2
    Tip: you can use the console to log your messages instead of alert, the command is: console.log("my message"). In firefox or Chrome you can press F12 to open the console. The error messages show up in the console as well. Commented Jul 13, 2013 at 2:04
  • 1
    And the error is: TypeError: sliderDirection[i] is undefined no mystery, just an error in your JavaScript. Commented Jul 13, 2013 at 2:06
  • Oh, wow! Well, it says "Uncaught TypeError: Cannot call method 'indexOf' of undefined" but that doesn't make sense! It isn't undefined. Additionally, I don't see how this could be the culprit because it still continues looping after that. Commented Jul 13, 2013 at 2:06
  • Wait...I get it. I should have done less than instead of less than or equal to. Not sure why it still continues with the loop, but... Commented Jul 13, 2013 at 2:06
  • That did it! Thanks for the help. jsfiddle.net/k7cSE/2 Commented Jul 13, 2013 at 2:07

1 Answer 1

2

Using a "<" in your for loop instead of a "<=" fixes your problem....

Sign up to request clarification or add additional context in comments.

Comments

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.