Traverse Array Beyond Length

Asked

Viewed 28 times

-1

I have the following array:

const array = ["A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"]

The array has 12 positions, I need that from the "A", can go through and go "beyond", for example, if I were to enter position 15, I would need to take the value "B".

for (i = 0; i < 16; i++) {

       
      const arr = ["A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"]
      arr[i % arr.length];
      console.log(arr[i % arr.length])
  }

In that case I had the return of:

A-A#-B-C-C#-D-D#-E-F-F#-G-G#-A-A#-B-C

But when I try to get position 13 for example, which would be A#, I get Undefined

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.