Answers for "9.5.2. Reversing a String¶ // Loops"

0

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
Posted by: Guest on June-17-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language