9 5 2 reversing a string c2 b6 2f 2f loops

Solutions on MaxInterview for 9 5 2 reversing a string c2 b6 2f 2f loops by the best coders in the world

showing results for - "9 5 2 reversing a string c2 b6 2f 2f loops"
Ambrine
08 Feb 2020
1/*We'll start by initializing two variables: the string we want to 
2reverse, and a variable that will eventually store the reversed value 
3of the given string.*/
4
5let str = "blue";
6let reversed = "";
7
8/*Here, reversed is our accumulator variable. Our approach to reversing 
9the string will be to loop over str, adding each subsequent character 
10to the beginning of reversed, so that the first character becomes the
11last, and the last character becomes the first.*/
12
13let str = "blue";
14let reversed = "";
15
16for (let i = 0; i < str.length; i++) {
17   reversed = str[i] + reversed;
18}
19
20console.log(reversed);
21
22//eulb