getArrayElements() with continouis addressing

36 views Asked by At
function getArrayElements(number, startIndex) {
  let result = [];
  // TODO: implement me
  if(startIndex > [...array].lenght){
    startIndex = startIndex % [...array].lenght
    result = [...array].slice(startIndex,startIndex+number)
    return result;
  } else {
    result = [...array].slice(startIndex,startIndex+number)
    return result;
  }
  
}

Task: Implement the function getArrayElements() that reads N elements from the array starting from the given start index i. The function takes two parameters (number and startIndex). The parameter startIndex can be greater than the length of the array. Implement a continuous addressing by looping through the array from the beginning again.

Example: Let L be the array length. The element with index 0 also has the index L, 2 * L, 3 * L, and so on. The parameter number indicates how many elements should be read. Reading stops at the end of the array even if the desired number of elements has not been read. (1 point)

I already have the solution for the case where the start index is not greater than the array length. I'm only missing the part where start index is greater. I tried using the modulo operator, but it does not work. [](https://i.stack.imgur.com/hqYg6.png)

0

There are 0 answers