Tell us what’s happening:
why is my code giving undefined when i console.log it .
also if i get
correct answer it wont pass the test cases . kindly help Your code so far
let arr = "";
function reverseString(str) {
for(let i=str.length;i>=0;i--){
arr +=str[i];
}
return arr;
}
console.log(reverseString("hello"));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36.
You’ve got a global variable. The let arr = "" should not be outside the function
What basically happens with that global variable is that the first test runs, passes. arr is now “elloh”. Next test runs, you add more characters on, so you get “ellohydwoH”, test fails, next test runs, add on and so on.