Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2013
    Thanked 0 Times in 0 Posts

    Palindrome problems

    I have this for class. We can't use reverse string. I'm getting 2 errors in firefox console, but don't know how to fix them.

    -ReferenceError: word is not defined java6.js:4

    -A form was submitted in the windows-1252 encoding which cannot encode all Unicode characters, so user input may get corrupted. To avoid this problem, the page should be changed so that the form is submitted in the UTF-8 encoding either by changing the encoding of the page itself to UTF-8 or by specifying accept-charset=utf-8 on the form element.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title> Assignment 6</title>
    <script type="text/javascript" src="java6.js"> </script>
    <form >
      Enter a String/Number: <input type="text" id="str" name="string" /><br />
      <input id="userButton" type="submit" value="Check" onclick="Palindrome();"/>
    Javascript Code:

     function Palindrome() {
    	var str = document.getElementById("str").value;
    	var i; 
    	for (i = 0; i < word.length; i++) {
    		if (word.charAt(i)!= word.charAt(word.length-1-i)) {
    		return false; 
    	return true; 
    window.onload = function() {
    	document.getElementById("userButton").onclick = function(evt) {

  2. #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    London, England
    Thanked 2,703 Times in 2,679 Posts
    for (i = 0; i < word.length; i++) {

    As your error console says, word is not defined. You have used str previously.

    But it is a bad idea to use the same name for an HTML element and a Javascript variable.

    Change your code to

    var word = document.getElementById("str").value;

    You are not submitting the form to anything, so your userButton should be type button.

    I am unclear what your code is aiming to achieve. Are you trying to identify if a given phrase is a palindrome, e.g. "Able was I ere I saw Elba"? If so you need to convert the string to lower case, and remove spaces and punctuation.

    <script type = "text/javascript">
    var pal = "Able was I ere I saw Elba!";
    // var pal = "Senile felines";
    var palstrip = pal.toLowerCase().replace(/[^a-z0-9]/gi,"");
    var palsplt = palstrip.split("");  // split into an array
    var len = palsplt.length;
    var lenx = parseInt(palsplt.length/2);
    var OK = true;
    for (var i = 0; i<lenx; i++) {
    if (palsplt[i] != palsplt[len-i-1]) {
    OK = false;
    if (!OK) {
    alert ('"'+ pal + '"' + " is not a palindrome");
    else {
    alert ('"'+ pal + '"' + " is a palindrome");
    Label on Sainsbury's peanuts -- "Warning: contains nuts."
    Last edited by Philip M; 01-04-2014 at 10:20 AM. Reason: Correction

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts