Get alternate values of an array

Viewed 3131

Could someone tell me how to get alternate values from a range of values, through Angular js?

monthDataCreation(){
    var startDay =1;
    var endDay = 10;
    for (var a = startDay; a < endDay; a++) {
        var element = a;
        console.log("list values like 1,2,5,7,9... ")
    }
} 

what I need is if I set a start value and end value and on loop it I should get alternate values.

If it start with even num 2 ends at 10 then the string of alternate value should be 2,4,6,8,10.

If it start with odd num 1 ends at 10 then the string of alternate value should be 1,3,5,7,9

Is there any angular way of solution

4 Answers
const a = [1, 2, 3, 4, 5, 6]
    for (let i = 0; i < a.length; i++) {
        if (i % 2 == 0) {
            console.log(a[i])
        }
    }
Related