Sreekanth Reddy
Sreekanth Reddy

Reputation: 483

Print all possibilities by rotating a string in Javascript

How to rotate a string in javascript and print the rotated versions of string without using any javascript functions, only for loops.

Given a string:

"hell"

Output:

"lhel", "llhe", "ellh", "hell"

I have tried but not succeded

    var str1 = "hell";

    
    let i = 0;
    let len = str1.length - 1;
    let temp;
    
    for (let j = 0; j < len+1; j++) {
        temp = str1[len]
        while (i < len) {
            console.log(str1[i]);
            temp += str1[i];
            i++;
        }
        console.log(temp);
        //console.log(typeof temp, typeof str1)
        str1 = temp;
    }

Upvotes: 2

Views: 1146

Answers (6)

GottZ
GottZ

Reputation: 4947

i know this has already been answered so.. this is how i'd do it.
maybe you can learn from it.

const str = "hell";

// this loop will set the offset. so if it's 1, "hell" will become "ellh"
for (let offset = 0; offset < str.length; offset++) {
  // this will contain the final string
  let output = "";

  // here we iterate through all the characters
  for (let index = 0; index < str.length; index++) {
    // and use modulo to switch 5 to 1 (in case length is 4)
    output += str[(index + offset) % str.length];
  }

  // there we go
  console.log(output);
}

Upvotes: 0

Andam
Andam

Reputation: 2177

You can try this method. Basically you have two loops the first loop with (i) is for the possibilities the second one is for the shifting

var strValue = "hell";
var temp;
for(var i = 0; i < strValue.length; i++){
  temp = "";
  for(var j = 1; j < strValue.length; j++){
    temp += strValue[j];
  }
  temp += strValue[0]
  strValue = temp;
  console.log(strValue)
}

Upvotes: 2

Nina Scholz
Nina Scholz

Reputation: 386736

You could take a nested loop and get the characters at the position of i and j and take the reminder operator % for preventing characters outside of the string.

var string = "hell",
    i, j,
    temp;

for (i = 0; i < string.length; i++) {
    temp = '';
    for (j = 1; j <= string.length; j++) temp += string[(i + j) % string.length];
    console.log(temp);
}

Upvotes: 2

sopan mittal
sopan mittal

Reputation: 45

let str = 'hell';



for(let i=0;i<str.length;i++){
	str = str.substring(1,str.length) + str[0];
	console.log(str);
}

Upvotes: -2

Cid
Cid

Reputation: 15247

You are almost there ! There is one thing missing, i should be reset at each iteration of the for loop, otherwise, the while (i < len) will be "played" only once :

var str1 = "hell";

let len = str1.length - 1;
let temp;
    
for (let j = 0; j < len+1; j++) {
    let i = 0;  // <-------------------- notice this
    temp = str1[len]
    while (i < len) {
        //console.log(str1[i]);
        temp += str1[i];
        i++;
    }
    console.log(temp);
    //console.log(typeof temp, typeof str1)
    str1 = temp;
}

Upvotes: 3

Mikhail Katrin
Mikhail Katrin

Reputation: 2384

In first loop add all symbols which indexes are greater then amount of shift steps (i in this case).

And add rest symbols after it the second loop.

const str = 'hello';

for (let i = 0; i < str.length; i++) {
  let shifted = '';

  for (let j = i; j < str.length; j++) {
    shifted += str[j];
  }
  
  for (let j = 0; j < i; j++) {
    shifted += str[j];
  }
  
  console.log(shifted);
}

Upvotes: 0

Related Questions