Rotate Left

by SeasonEnds

JavaScript

// 1. figure out what you need to do
// You are given an array of integers and the number of left roations to perform.
// You've got to return the array printed as a single line space separated integers

// 2. Figure out what the array looks like and how many left rotations you need to do
// Size of the array = 5
// Number of left rotations to be performed = 4

// Complete the existing function

function rotLeft(array, rotations) {
  console.log('initial array is', array)
  console.log('rotations is', rotations)

  // rotate it
  for (var i = 0; i < rotations; i++) {
  	array.push(array.shift());
    console.log(array)
  }
  
  // return it as a single line of space separated integers
  console.log(array)
  return array;
}

// pass in the variables and execute the function
rotLeft([1,2,3,4,5], 4)