1

I'm new to Javascript and I'm trying to create a function that rotates the array depending on how many times number is === to, using splice. I have come up with this so far but I face an undefined error:

    var count = 0
    function rotate(arr, num) {
     
        while (count < num)
        {
            arr.splice(0,0, arr.indexOf(3));
            arr.splice(4,1);
            count++
        }
    }
    console.log(rotate(["Harry", "Sarah", "Oscar", "Tina"], 2));
Vijay
  • 5,331
  • 10
  • 54
  • 88
Edwards
  • 105
  • 2
  • 7
  • I would appreciate it if someone could help, the solution I am looking for is not like the ones offered in other questions. I just want to know why it is undefined and if I'm doing this in the right way, please help? – Edwards Apr 21 '15 at 14:34
  • Because you aren't returning the result. Add `return arr` to the bottom of your `rotate` function. – Mike Cluck Apr 21 '15 at 14:40
  • indexOf(3) is searching for the number 3 in the array. It is not getting the element at index 3. You could instead write this `arr = arr.concat(arr.splice(0, 1))` to rotate left or `arr = arr.splice(arr.length-1, 1).concat(arr)` to rotate right. – Tesseract Apr 21 '15 at 14:41

0 Answers0