Community

Ask A Question


Notifications
Subscribe You’re not receiving notifications from this thread.

Validate Palindrome

Challenges • Asked 9 months ago by Jake from AlgoDaily

Gravatar Christian Austria Commented on Jul 05, 2019:

I think for the solution you provided, we must account for the case of the character or letter as well as the whitespaces.

So here's my solution:

/* jshint esversion: 6 */

function isPalindrome(str) {
    if (!isAlphaNumeric(str)) {
        // return false;
    }

    const reverseStr = Array.from(removeSpaces(str)).reverse().join('').toLowerCase();

    return reverseStr === removeSpaces(str).toLowerCase();
}

function isAlphaNumeric(c) {
    // create a regex pattern for alphanum
    const reAlphaNumeric = /[a-z0-9]+$/i;
    return reAlphaNumeric.test(c);
}

function removeSpaces(str) {
    return str.split(' ').join('');
}

console.log(isPalindrome('A Santa Lived As a Devil At NASA'));
Terms and Conditions | Privacy