How Do You Reverse a String?
Steps:
- Create a empty string
newString
- Create a for loop, the starting point of the loop with be
str.length - 1
which will be the last character of the string - Continue for loop till
i
is greater than or equals0
, decrementi
after each iteration
function reverseString(str) {
let newString = "";
for(let i = str.length - 1 ; i >= 0; i--) {
newString = newString + str[i];
}
return newString;
}
reverseString('hello');