9.5.2. Reversing a String¶ // Loops
/*We'll start by initializing two variables: the string we want to
reverse, and a variable that will eventually store the reversed value
of the given string.*/
let str = "blue";
let reversed = "";
/*Here, reversed is our accumulator variable. Our approach to reversing
the string will be to loop over str, adding each subsequent character
to the beginning of reversed, so that the first character becomes the
last, and the last character becomes the first.*/
let str = "blue";
let reversed = "";
for (let i = 0; i < str.length; i++) {
reversed = str[i] + reversed;
}
console.log(reversed);
//eulb