4

I have an array ["#page1", "yield", "yield", "yield", "#page2", "yield", "#page3" ]

I want to iterate through the array top-down, starting at the last yield and looking for the next occurence of an element not being yield (which is #page2).

This is what I have:

var longest = ["#page1", "yield", "yield", "yield", "#page2", "yield", "#page3" ],
    longestLen = longest.length;
for (i = longestLen-2; i>=0; i--) { 
     if ( longest[i] != "yield") {
          var gotoPage = longest[i];
          }
    }

I'm starting at i=5 (6 iterations) and checking if the element is not yield. However, the current way runs through all 6 iterations, so I end up with the page#1 instead of the #page2. I don't know how I can stop the iterations.

Using return did not work, what other means are there? Is lastIndexOf something I could use?

Thanks for help!

1

4 Answers 4

6

Just because noone mentioned it so far, you can also go with ES5 .some() for that.

Looks like:

var longest = ["#page1", "yield", "yield", "yield", "#page2", "yield", "#page3" ],
    gotoPage;

longest.reverse().slice(1).some(function( elem ) {
    return (gotoPage = elem) !== "yield";
});

alert('goto page: ' + gotoPage);
Sign up to request clarification or add additional context in comments.

6 Comments

Nice solution, but this does not start at the last yield does it?
@pimvdb: since I'm calling .reverse() before .some() it will!
But if I run this code I obtain #page3 for gotoPage, because .some already stops at #page3. Unless I'm mistaking, the OP wants to start at the last yield.
Correct. I can't start at the last entry, because that is the page I'm currently on (on panel-x). "yields" are added to the array when page changes on a correspondig panel (y). In order to get a backward-transition to the proper page, I need to find the last non-"yield" entry on the panel with the currently active page (= #page3). break works fine for me, although I will keep this mind, too
well, I didn't realize that. If its always 100% sure that you need to skip the very last entry, I extended the snippet with .slice(1) which pretty much does the job.
|
4

you're looking for the breaktutorial keyword, which breaks out of a loop.

if ( longest[i] != "yield") {
    var gotoPage = longest[i];
    break; // wont iterate any further in the for(...) loop
}

Comments

0

Try 'break' -

var longest = ["#page1", "yield", "yield", "yield", "#page2", "yield", "#page3" ],
    longestLen = longest.length;
for (i = longestLen-2; i>=0; i--) { 
     if ( longest[i] != "yield") {
          var gotoPage = longest[i];
          break;
          }
    }

Comments

0

Have a look at the 'break' operation with labels, so that you can break out where ever you'd like.

Javascript Loop Control with Break and Continue

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.