0

Here's the function I have written right now. The web console isn't doing much for me. Every time a word is entered in the text box, even if it is a palindrome, it just returns "undefinedThis is not a palindrome!".

Not sure what's wrong here. Perhaps something to do with the if statement? How can I make it so that the function ignores capitalized letters? I want it to consider both "bob" and "Bob" a palindrome.

function test() {
    // Assumes: a string is entered into the "palin" text box
    // Results: tests if the string is a palindrome and
    // returns the conclusion to outputDiv

    var word, copy, i;
    word = document.getElementById("palin").value;
    copy = " ";
    i = 0;

    while (i < word.length) {
        copy = word.charAt(i) + copy;
        i=i+1;
    }

    if (word === copy) {
        document.getElementById("outputDiv").innerHTML =
            document.getElementById("outputDiv").value +
            "This is a palindrome!";
    } else {
        document.getElementById('outputDiv').innerHTML =
            document.getElementById('outputDiv').value +
            "This is not a palindrome!";
    }
}
3
  • To solve capital-simple mismatching You can convert the whole string to lowercase letters before comparison begins. w3schools.com/jsref/jsref_tolowercase.asp Commented Dec 7, 2014 at 4:43
  • You should add some console.log() or alert() statements to debug the values of word and copy as your program executes. The problem is probably that you initialize copy to ' ' (a space) instead of the empty string, ''. But you need to learn how to debug your own code, so be sure to give the logging a shot. Commented Dec 7, 2014 at 4:45
  • @user2570380 No. You would have to do (word.split("").reverse().join("").toLowerCase() === word.toLowerCase()). Commented Dec 7, 2014 at 5:00

2 Answers 2

1

It's best if you separate your logic from your presentation. In your case the logic is the palindrome check. The presentation is getting an input from text box and sending the output to the div.

This is what I would do:

var input = document.getElementById("input");
var output = document.getElementById("output");

document.getElementById("button").addEventListener("click", function () {
    var s = input.value;
    output.innerHTML = s + " is" + (isPalindrome(s) ? "" : " not")
                     + " a palindrome.";
});

function isPalindrome(s) {
    s = s.toLowerCase();
    return s.split("").reverse().join("") === s;
}
<input type="text" id="input"/>
<button id="button">Palindrome?</button>
<div id="output"></div>

As you can see I have put the isPalindrome logic into a separate function. Hence I have separated the logic from the presentation. You should always do this. It makes programming much simpler.

Sign up to request clarification or add additional context in comments.

Comments

0

your code is almost correct .here is fixed jsfiddle example

change your codes to

function test() {

    var word, copy, i;
    word = document.getElementById("palin").value.toLowerCase();//turn to lowercase
    copy = "";
    i = 0;

    while (i < word.length) {
        copy = word.charAt(i) + copy;
        i=i+1;
    }

    if (word === copy) {
        document.getElementById("outputDiv").innerHTML =
            document.getElementById("palin").value + " This is a palindrome!";
    } else {
        document.getElementById('outputDiv').innerHTML =
            document.getElementById('palin').value + " This is not a palindrome!";
    }
}

copy = " "; should change to copy = ""; that's first problem because "bob" not equal to "bob " you used document.getElementById("outputDiv").value which is undifined because it's a div not a textarea so value is undifined .what you probably need is document.getElementById("palin").value

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.