Hello!
This is an exercise from W3resource. I am trying to work out the problem using my own thought logic.
The goal of the exercise is to keep removing the last letter in the word 'w3resource' and add it to the front of 'w3resource'. This is supposed to continue until you close the browser.
I am able to get it done, but when I reach the end of the array br, I get undefined. I then
tried set if(br || br[br.length-1]), but I guess array br is still empty so it can't go through
it again.
Using my code, can you please help me get the same effect as the original assignment:
JavaScript basic: Rotate a string from left to right - w3resource[
^]
Here is my code:
function animate_string(word){
var br = word.split('');
if(br || br[br.length-1]){
setInterval(function(){
var show = br.pop() + br.slice(0).join('');
document.getElementById('target').innerHTML = show;}, 500);
}
}
animate_string('w3resource');
What I have tried:
I am able to get W3rescource to rotate once by removing the last letter and adding to the front of the text, but when it reaches the end of the 'array br', I get undefined. I then
tried set if(br || br[br.length-1]), but I guess array br is still empty so it can't go through
it again.