Reverse string algorithm

Can anyone plz explain why my code getting error; thanks

function print(msg) {
  return msg;

function reverseString(str) {
  var result = "";
  for(let i=str.length; i>=0; i--) {
  result += str[i];
  return result;

//output undefinedolleh 

The first thing you are adding to result is undefined, so str[str.length] is undefined. Why might that be? Use “hello” for example, what is “hello”[5]?

1 Like

I got it.
Thanks heaps

I saw this function today you need to add -1 to the condition for where there is let i = stri.length

Thanks mate i already done.

have you tried the split() and join() JS functions?


var v1 = str.split()
var v2 = str.reverse()
return v2.join()

Actually i did and complete the challenge.

Oh, okay. Glad to hear that.

1 Like