I'm trying to write code to determine if a string is a palindrome. I am making the string lowercase, taking out the spaces, and turning it into an array. Next, I am splitting it in half, reversing the second half, and comparing those two arrays to see if the string is a palindrome. The function will not log true.
let string = "Never odd or even";
let lowerString = string.toLowerCase();
let split = lowerString.split("");
let array = split.filter(noSpaces);
function noSpaces(i) {
return i !== " ";
}
function checkIfPal() {
if (array.length % 2 === 1) {
let firstHalf = array.slice(0, array.length / 2);
let secondHalf = array.slice(array.length / 2 + 1, array.length);
let revSecondHalf = [];
for (let i = secondHalf.length - 1; i > -1; i--) {
revSecondHalf.push(secondHalf[i]);
}
if (firstHalf === revSecondHalf) {
console.log("true for odd");
} else {
console.log("false for odd");
}
} else {
let firstHalf = array.slice(0, array.length / 2);
let secondHalf = array.slice(array.length / 2, array.length);
let revSecondHalf = [];
for (let i = secondHalf.length - 1; i > -1; i--) {
revSecondHalf.push(secondHalf[i]);
}
if (firstHalf === revSecondHalf) {
console.log("true for even");
} else {
console.log("false for even");
}
}
}
checkIfPal();