I wanted to check if a string is palindrome, but I have a problem in reversing a string. I know there's a lot of Questions about palindrome in Javascript, but i want to find it on my way and I still can't find the solution on those Questions.
Code
function reverse(string){
var str = string;
var split = str.split("");
var newStr= "";
for(var i=split.length; i>=0; i--){
newStr += split[i];
}
console.log(newStr);
}
var str= 'blue';
reverse(str);
Results
"undefinedeulb"
Question
As we can see, the results was an undefined value at the start of the string. I tested this code with PHP script, and It works fine. How could this be a problem with Javascript? and How to get rid of it?
str.split("").reverse().join("")reverses a stringsplit[split.length]which is undefined. You are off by one