Tell us what’s happening:
I already reversed the string, str equals the reversed string so why isn’t this working? Ignore the console.logs was just checking
Your code so far
function reverseString(str) {
let arr= []
for (let i=0; i<str.length; i++){
arr.unshift(str[i])
}
let result = arr.reduce((sum, current) => sum + current);
str = result
console.log(str)
console.log(arr)
}
reverseString("hello")
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36.
Hi,
You have to remember to return something. Console.log prints the result to the console, but “return” is Javascript’s way of saying “Stop executing this function, and make the result available outside of this function.”
you just need to split it with method str.split(' ') method and then just reverse array and join str ;
or you can apply for loop from last index of str array to get reversed string