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
  • 167
  • 3
  • 8

1 Answers1

5

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
  • 376,160
  • 25
  • 347
  • 392