Can anyone plz explain why my code getting error; thanks
function print(msg) {
console.log(msg);
return msg;
}
function reverseString(str) {
var result = "";
for(let i=str.length; i>=0; i--) {
result += str[i];
}
return result;
}
print(reverseString("hello"));
//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
undefined
I got it.
Thanks heaps
camcode
September 26, 2019, 12:53pm
4
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?
Answer
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