Palindrome

Check if a word is a palindrome

by Marcus Baptiste

HTML

<h2> Palindrome Testing Program </h2>

<p>A palindrome is a word or phrase spelled the same way in reverse.
    <br>
    <br>Enter a word or phrase and click "Check" to see if it is a palindrome.
    <br>Try the different options!</p>
<input type="text" id="test">
<button id="btntest">CHECK</button>
<br>
<br><input type="checkbox" id="caseSen"> Case Sensitive:
<br>
<br><input type="checkbox" id="spaces"> Remove Spaces
<br>
<br> <input type="checkbox" id="onlyLet"> Only Letters

JavaScript

function isPal(testText) {
    var ispal = true,
        testLength = 0,
        halfLength = 0;
    testLength = testText.length;

    halfLength = Math.floor(testLength / 2);

    for (i = 0; i < halfLength; i++) {
        if (testText[i] != testText[testLength - i - 1]) {
            ispal = false;
            break;
        }
    }
    return ispal;
}

function testString() {
    
    var inputText = document.getElementById("test").value,
        newText = '',
        caseOpt = document.getElementById("caseSen").checked,
        spaceOpt = document.getElementById("spaces").checked,
        onlyLetOpt = document.getElementById("onlyLet").checked;

    if (inputText === '') {
        alert('Please enter some text!');
        return false;
    }
    
    newText = inputText;
    
    //case sensitive option 
    if (!caseOpt) {
        newText = newText.toLowerCase();
    }

    //white space option
    if (spaceOpt) {
        newText = newText.replace(' ', '');
    }
    
    //only letters option
    if (onlyLetOpt) {
        newText = newText.replace(/[^a-zA-Z\s]/g,'');
        alert(newText);
    }

    if (isPal(newText)) {
        alert('yes I am a palindrome!');
    } else {
        alert('I am not a palindrome!');
    }
}



var btn = document.getElementById("btntest");

if (btn.addEventListener) {
    btn.addEventListener('click', testString, false);
} else {
    btn.attachEvent('onclick', testString);
}