I am a beginner in java-script. I am writing code that returns true or false for palindrome string. I know there are lot of ways to write code to check palindrome yet I want to know what went wrong in my code. please help.
function palindrome(str) {
if (str == "") {
return false;
} else {
const palia = str.replace(/[\W_]/g, "").toLowerCase();
const number = palia.length;
console.log(number);
var num1 = number / 2;
num1 = Math.floor(num1);
console.log(num1);
for (var i = 1; i < num1; i++) {
var b = number - i;
b++;
console.log(b);
if (palia[i] == palia[b]) {
console.log("Ture");
return true;
} else {
console.log("false");
alg = "false";
return false;
};
};
};
};
console.log(palindrome("eye"));
And I know i have written lengthy code. My apologies. I will learn other ways to write. But this is exiting with code=0. at for loop. Please help. Thanks in advance.