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

    Need help with .split and .charCodeAt

    I am trying both .split and .charCodeAt for the first time. When I put them into one test program it does not even give me the prompt I put into the program to serve as the input, just a blank white page. I wanted the program to tell me if the last letter of the first word is "e" or not, but anyways, here is the short bit of code i wrote for it:
    Code:
    <script language="text/javascript" type="text/javascript">
    var input = prompt("Your text goes here","the cow says moo");
    var lower = input.toLowerCase();
    var words = lower.split(" ");
    
    if (words[0].charCodeAt(-1); == "e") {
    
    	var statement = words[0] + "ends in 'e'";
    }
    document.write(statement);
    </script>
    Last edited by stairway; 05-15-2009 at 02:32 AM.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    Bunch of stuff there ...

    <script language="text/javascript" type="text/javascript">

    Drop the language attribute (it's just "JavaScript" anyway).
    Mimetype is plenty.

    if (words[0].charCodeAt

    charCodeAt? That'll get you the ... erm ... character code. Looks like you want: the character.

    (-1); == "e")

    No idea why the -1.

    This what you were trying to do?

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>untitled</title>
    </head>
    <body>
    <script type="text/javascript">
    
       var input = prompt("Your text goes here","the tree is very free");
       var lower = input.toLowerCase();
       var words = lower.split(" ");
       var word;
       for (var i = 0; i < words.length; i++)
       {
          word = words[i];
          if (word.charAt(word.length - 1) == "e") 
          {
             document.write("<strong>" + word + "</strong> ends in 'e'");
             document.write("<br />");
          }
       }
    
    </script>
    </body>
    </html>
    Cheers!

  • Users who have thanked adios for this post:

    stairway (05-15-2009)

  • #3
    New to the CF scene
    Join Date
    May 2009
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks for the help!


  •  

    Posting Permissions

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