Javascript sentence parser

Discussion in 'OT Technology' started by elevator, Mar 18, 2007.

  1. elevator

    elevator The tenants think it's wonderful! ヽ(´ OT Supporter

    Joined:
    Mar 25, 2005
    Messages:
    10,967
    Likes Received:
    0
    Location:
    Houston, TX
    My code:

    Code:
     
    <SCRIPT LANGUAGE="JavaScript">
    <!--
     sInput = window.prompt("Enter a sentence", "");
     x1 = 0;
     x2 = 0;
     var temp = new Array();
     temp = sInput.split(' ');
    
     document.writeln("<table border="1"><tr>");
     
     i = 0;
     while (i <= temp.length)
     {
     i++;
     document.writeln("<td>" + temp[i] + "</td>");
     }
     document.writeln("</tr></table>"); 
    // -->
    
    It is code to take a sentence and split it into words (using the SPACE as a trigger) and then list each word in a table. I have stored the words to an array, I know this part is right after testing it, but when I add the while loop to put the words in a table, nothing shows up on my page...

    Anyone see anything wrong :hs:
     
  2. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Does not look like you instantiate sInput, x1, x2 or i. I always instantiate these... maybe you don't have to though?

    Also... why is all your code in a commented out area? Anything inside <!-- --> will not run.

    And do you have firefox with firebug installed? It should tell you if there is an error.
     
  3. peerk

    peerk New Member

    Joined:
    Mar 14, 2005
    Messages:
    984
    Likes Received:
    0
    You forgot to escape your quotes in line 9. Change that and it will give output.

    And there are 2 other errors.


    Code:
    <SCRIPT LANGUAGE="JavaScript">
    <!--
     sInput = window.prompt("Enter a sentence", "");
     x1 = 0;
     x2 = 0;
     var temp = new Array();
     temp = sInput.split(' ');
    
     document.writeln("<table border=\"1]\"><tr>");
     
     i = 0;
     while (i <= temp.length)
     {
     i++;
     document.writeln("<td>" + temp[i] + "</td>");
     }
     document.writeln("</tr></table>"); 
     // -->
    
    </SCRIPT>
    
     
    Last edited: Mar 19, 2007
  4. elevator

    elevator The tenants think it's wonderful! ヽ(´ OT Supporter

    Joined:
    Mar 25, 2005
    Messages:
    10,967
    Likes Received:
    0
    Location:
    Houston, TX
    thanks peerk, got it to work. I got the other two errors - incrementing too early in the loop and going through one too many times (also added <tr></tr> tags in the loop to make it multiple rows instead of columns)

    its always easier to debug if you get an output :bigthumb:

    what is the ] for in the code you posted for escaping the quotes?

    thanks again!
     
  5. peerk

    peerk New Member

    Joined:
    Mar 14, 2005
    Messages:
    984
    Likes Received:
    0
    That's a typo.

    Looks like my fingers are getting fat :rofl:
     
  6. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    It looks like you're trying to write javascript code without an IDE that will check your syntax, and without Firebug to debug errors.

    Don't do that.
     
  7. elevator

    elevator The tenants think it's wonderful! ヽ(´ OT Supporter

    Joined:
    Mar 25, 2005
    Messages:
    10,967
    Likes Received:
    0
    Location:
    Houston, TX
    suggest a good IDE? We haven't learned about anything other than opening a text editor and writing javascript...
     
  8. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Google 'javascript ide.' There are lots. I use Eclipse with a javascript plugin.
     
  9. gnp

    gnp New Member

    Joined:
    Mar 11, 2007
    Messages:
    280
    Likes Received:
    0
    there isn't a really great javascript ide with integrated debugging

    i used to use eclipse, i switched to aptana a few months ago and it is based on eclipse and still full of bugs (best js ide i have found though). i use venkman to debug:dunno:

    http://www.aptana.com
     
  10. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    The debugging is firebug. Yeah, it should be integrated.
     
  11. sprite

    sprite Active Member

    Joined:
    May 20, 2003
    Messages:
    2,967
    Likes Received:
    0
    Location:
    MI, USA

Share This Page