Please help me with this Program

Write a JavaScript program to rotate the string in right direction by periodically removing one letter from the end of the string and attaching it to the front.

Here is my code:


function rotate() {

var string = "sentence";

var splitted = string.split("");

var newarr = [];

for (var i=splitted.length; i=0; i--) {

	newarr = splitted.pop();
return newarr;

console.log (rotate());


Am I doing anything wrong in the logic of the For loop?

Thank you

The condition i = 0 isn’t fulfilled when it starts, so it exits right away.

1 Like

Yep, when counting down with a for loop you want the condition to be i > 0 e.g.

for (var i=splitted.length; i>0; i--) {}
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.