Skipping repeated every 5th in line

Viewed 49

I try to using continue in for loop to skip every 5th iteration. But, I just know how to write the continue in which just skip the first 5th. Not sure how I can implement the continue statement to make it work. The correct output should be:

1  is odd.
2  is even.
3  is odd.
4  is even.
6  is even.
7  is odd.
8  is even.
9  is odd.
11  is odd.
12  is even.
13  is odd.
14  is even.
16  is even.
17  is odd.
18  is even.
19  is odd.

Here is my code.

forwardOp(1,20)
function forwardOp(startIndex, endIndex){
    if (startIndex > endIndex){
        console.log("Start number cannot be less than End number.");
    }
    for (i = startIndex; i <= endIndex; i++){
        if (i === 5){continue;}
        if (i % 2 ==0){
            console.log (i," is even.")
        }else {
            console.log (i, " is odd.")
        }
    }
}
3 Answers

Same as module 2

if (i % 5==0){
            console.log (i," is divisible by 5.")
        }

You must use i % 5 == 0.

Here is the fixed code to make it easy:

forwardOp(1,20)
function forwardOp(startIndex, endIndex){
    if (startIndex > endIndex){
        console.log("Start number cannot be less than End number.");
    }
    for (i = startIndex; i <= endIndex; i++){
        if (i % 5 == 0){
            // 'i' is divisible by 5
        }
        if (i % 2 ==0){
            console.log (i," is even.")
        } else {
            console.log (i, " is odd.")
        }
    }
}

The line if (i === 5){continue;} means that the 5th line gets skipped over. If you want to skip every 5th line you should use modulo: if (i%5 == 0){continue;}

Related