1

Let's say we have an Array like so:

var example = ["a", "b", "c", "d", "e"]

We know that we can access an index of 0-4:

example[0] == "a"
example[1] == "b"
example[2] == "c"
example[3] == "d"
example[4] == "e"

However, we also want to map an undefined index. For example:

example[0] == example[5] == example[10] == example[15] == example[20] == "a"
example[1] == example[6] == example[11] == example[16] == example[21] == "b"
example[2] == example[7] == example[12] == example[17] == example[22] == "c"
example[3] == example[8] == example[13] == example[18] == example[23] == "d"
example[4] == example[9] == example[14] == example[19] == example[24] == "e"

Though it may be simple, the math is simply escaping my mind. Keep in mind the example.length is not fixed and may be different at times. How would one go about this?

1 Answer 1

7

You seem to want to use the modulo :

example[i%example.length]
Sign up to request clarification or add additional context in comments.

1 Comment

Well that was quick! I forgot about the modulo.

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.