JSFiddle - React, Tailwind, and code Playground

by ronilan

JavaScript

// this will evaluate a string and return the number of times a letter is not where it should be in a palindrome

function palindromeErrorCount(inString) {

    var reverseString,
        result = 0,
        i,
        max;

    reverseString = inString.split("").reverse().join("");
    max = reverseString.length;

    for (i = 0; i < max; i++) {

        if (reverseString.substr(i, 1) !== inString.substr(i, 1)) {
            result++;
        }
    }

    return result;
}

function stripSpacesAndPunct(inString) {
   
    return inString.replace(/ /g, "")
        .replace(/,/g, "")
        .replace(/!/g, "")
        .replace(/-/g, "");
}

var p = "A man, a plan, a cat, a ham, a yak, a yam, a hat, a canal-Panama!"

console.log(palindromeErrorCount(stripSpacesAndPunct(p.toLowerCase())))