
Is this string a Palindrome in JavaScript?
Self-written code without any tools or looking up anything to check if a string is a palindrome. Quickly written as an exercise, this is an interesting thing to solve. This isn’t optimized as it was written in 5-10 minutes or so. I added in the input field for this page later, only talking about the function itself.
Update - added in the toLowerCase so an input like “Racecar” is still True and it doesn’t match the exact case, just the letters.
This code is the javaScript function being used to evaluate this:
function isPalindrome(stringToEvaluate) {
stringToEvaluate = stringToEvaluate.toLowerCase()
if (stringToEvaluate === null || stringToEvaluate.length === 0) {
return false;
}
stringToEvaluate = stringToEvaluate.toString();
let s = 0;
let end = stringToEvaluate.length - 1;
while (s < end) {
if (stringToEvaluate[s] !== stringToEvaluate[end]) {
return false;
}
s++;
end--;
}
return true;
}
console.log(isPalindrome("12334321")) // false
console.log(isPalindrome("racecar")) // true
console.log(isPalindrome("145641")) // false
console.log(isPalindrome("123454321")) // true