How to Solve HackerRank’s Left Rotation Problem (JavaScript)

  1. Draw it out; and do it manually as a human would
  2. Take those steps, and write it out, or pseudo code it into steps
  3. THEN begin translating that into code.
  1. Take the first element of the array…
  2. Put in the back
  3. Repeat this as many times as d says.
function rotLeft(a, d) {   while (d) {      a.push(a.shift());      d--;   }return a;}
while (d)
d--
a.push(a.shift());

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store