This is 2nd exercise from Basic Algorithm Scripting - Reverse a string. Below is my approach to solve the problem.
function reverseString(str) {
let arr = [];
str = str.split("");
for(let i = 0; i < str.length; i++) {
a.push(str.pop());
}
str = a.join("")
return str;
}
reverseString("hello");
Unfortunately, it is not working. But, if I do this it is working
function reverseString(str) {
let a = [];
str = str.split("");
let b = str.length;
for(let i = 0; i < b; i++) {
a.push(str.pop());
}
str = a.join("")
return str;
}
reverseString("hello");
Someone please explain me what’s happening. I mean why is it not working when I am using i < str.length
but when I assign the same to a variable and use that variable instead of actual value its working. What am I missing?