Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

To finish the function scrollingText(word)

I am writing a program where I want the following result:

[ ‘ROBOT’,
‘OBOTR’,
‘BOTRO’,
‘OTROB’,
‘TROBO’ ]

Now I have:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[ ‘robotr’, ‘obotr’, ‘botr’, ‘otr’, ‘tr’ ]

Where am I going wrong? Here is my code:

function scrollingText(word) {
  word = word.toUpperCase();
  let arr = [];
  for (let i = 0; i < word.length; i++) {
    arr.push(word[i] + word.slice(i + 1) + word[0]);
  }
  return arr;
}

console.log(scrollingText('robot'));

>Solution :

You need to update word in each iteration, and not merely slice the same word repeatedly. Here is my snippet:

function scrollingText(word) {
  let arr = [word.toUpperCase()]; // storing original word
  let wordLength = word.length;

  for (let i = 0; i < wordLength - 1; i++) { // iterating for one less than the string length, in this case, from 0 to 3
    word = word.slice(1) + word[0] // <<-- update word in every iteration
    arr.push(word.toUpperCase());
  }

  return arr;
}

console.log(scrollingText('robot'));

// OUTPUT:
// [ "ROBOT", "OBOTR", "BOTRO", "OTROB", "TROBO" ]

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading