Estoy escribiendo un programa donde quiero el siguiente resultado:
[ 'ROBOT', 'OBOTR', 'BOTRO', 'OTROB', 'TROBO' ]
Ahora tengo:
[ 'robotr', 'obotr', 'botr', 'otr', 'tr' ]
¿Dónde me estoy equivocando? Aquí está mi código:
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'));Debe actualizar la word en cada iteración, y no simplemente slice la misma word repetidamente. Aquí está mi fragmento:
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'));