Already reversed the string but the answer is not being accepted

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.

Challenge: Reverse a String

Link to the challenge:

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.”

Hope that makes sense.

1 Like

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

yeah, you reversed the string, but does the function return something? or just consoles it?

return

join str

after for loop