2
function getAnyItem(arr, position) {
  if (position > arr.length) {
   return position = arr[0] ;
  }
  let index = arr[position];
  return index;

 }

I am really struggling on finding a way to go back round an array without using loops.Above is what I have written so far, all I get it undefined. Any help would be much appreciated as I am new to coding.

getItem(['a','b','c'], 10) should return 'b'

edwrdcodeu
  • 47
  • 6

1 Answers1

3

You could take the remainder operator % with the length of the array

function getItem(arr, position) {
   return arr[position % arr.length];
}

console.log(getItem(['a','b','c'], 10)); // 'b'
Nina Scholz
  • 351,820
  • 24
  • 303
  • 358