0

I have a for loop which goes through an array but I am trying to restrict it to break after the 2nd item.

var x="",i=0;
for (var x in marking;i=0;i++) {
    if (i==2){
        break;
    }
    index.push(marking[x]);
};

The console log error

SyntaxError: missing ) after for-loop control
    for (var x in marking;i=0;i++) {

Could someone please show me how to combine the

for (var x in marking)

and

for (i=0;i<10;i++)
ngplayground
  • 20,365
  • 36
  • 94
  • 173

4 Answers4

2

If you need to break after 2nd item, have you considered following approach:

for (var i=0;i<2;i++) {
   index.push(marking[i]);
};
Rohit Deshmukh
  • 130
  • 2
  • 6
0

Simply put the i++ inside your for in loop

var i=0;
for (var x in marking) {
    if (i==2){
        break;
    }
    index.push(marking[x]);
    i++;
};

NB: You don't need to declare the x variable before the loop since you re-declare it inside of it.

Skwal
  • 2,160
  • 2
  • 20
  • 30
  • 1
    You should not use `for (… in …)` to iterate over an Array: http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-such-a-bad-idea – TimWolla Mar 26 '14 at 15:45
  • It's probably not the best place to debate this, but I think as long as it's not misused, it's perfectly fine for use `for in` loops for Arrays as long as you don't mess up with the array prototype or change it on the fly. The link in your comment says the same. – Skwal Mar 26 '14 at 16:00
0

edit

Come to think of it, you don't need a loop to do this at all. You can just take a slice.

If index already contains something, you can use concat:

index.concat(marking.slice(0,2));

If index has been declared already but is empty:

index = marking.slice(0,2);

This will add the first two items of marking to the end of index.

previous answer - potentially unsafe

  1. There's no need to have var x in both places. In JavaScript, a declaration anywhere within a function means that the variable exists from the top of the function but is undefined until the line you have declared it. To help remember that, I normally put my var declarations at the top of the function.
  2. Put ++i (or i++, or i+=1) in your loop.
  3. Use === when comparing two things of the same type. == is slightly faster.

Like this:

var x,i=0;
for (x in marking) {
    if (i == 2){
        break;
    }
    index.push(marking[x]);
    ++i;
}

For conciseness you could even combine the increment and comparison, like:

var x,i=0;
for (x in marking) {        
    index.push(marking[x]);
    if (++i == 2){
        break;
    }        
}

Will break after the second item and only go through two iterations of the loop, rather than breaking on the third.

Community
  • 1
  • 1
Tom Fenech
  • 72,334
  • 12
  • 107
  • 141
  • Comparing two integers is slightly faster with `==` than `===`. In this case you know `i` is an integer so it's safe to use the `==`. (Source: http://jsperf.com/double-vs-triple) – Skwal Mar 26 '14 at 15:32
  • You should not use `for (… in …)` to iterate over an Array: http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-such-a-bad-idea – TimWolla Mar 26 '14 at 15:47
-1
var i=0;
for (var x in marking) {
    if (i==2){
        break;
    }
    index.push(marking[x]);
    i++;
};
tarmaq
  • 422
  • 2
  • 7
  • You should not use `for (… in …)` to iterate over an Array: http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-such-a-bad-idea – TimWolla Mar 26 '14 at 15:46