DEV Community

Cover image for Palindrome Interview Question Solution JavaScript

Palindrome Interview Question Solution JavaScript

Hello Dev World Blog on December 31, 2020

Another popular interview question is the palindrome test. These solutions are for simple 1 word palindromes. For more complicated palindromes with...
Collapse
 
shmulvad profile image
Søren Hougaard Mulvad

The code for solution 3 is not correct. As it is right now, it will only compare the very first and last character in the string. Therefore there will be a lot of false positives. You should do something along the lines of

function isPalindrome(stringToCheck) {
    const strLower = stringToCheck.toLowerCase();
    for (let i = 0; i < Math.floor(strLower.length/2); i++) {
        if (strLower.charAt(i) !== strLower.charAt(strLower.length - i - 1)) {
            return false;
        }
    }
    return true;
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
hellodevworldblog profile image
Hello Dev World Blog

shoot thank you for that catch i messed up in my testing when i was trying to make it smaller lol has been corrected with an explanation why it needs to be that way :)