Thursday, 16 March 2017

How to start over when there are no more elements in a loop?

Let's say I "want" 6 elements from an array which only contains 3. If the end is reached, start over.

Example:

let arr = ["a", "b", "c"];
let wanted = 5;

for(let i = 0; i < wanted; i++) {
    console.log(arr[i]);
}

Which of course returns:

a
b
c
undefined
undefined

Instead, I need:

a
b
c
a
b

Is there a way to start from the beginning of the array after the elements are over?



via Laurent

No comments:

Post a Comment