Javascript Programming

Discussion in 'OT Technology' started by z06, Sep 30, 2004.

  1. z06

    z06 New Member

    Joined:
    May 27, 2003
    Messages:
    141
    Likes Received:
    0
    I am having trouble figuring out the syntax to my program...although I have the logic for it, I can't seem to get the syntax right, so if any one knows how to to help me fix it, that'd be great!

    Basically I'm going to be creating a form with a textarea, a submit button calling the function count(), create a variable that will take in the user's text, and display an alert message showing the number of characters in the user's text. This program I got easily, but it is the second part that I need help with. I have to create a textbox where only one character can be entered, create a function called countchars(), where this function will be used to count how many times a character has been entered in the textbox above.

    Here is my code, thus far:

    <html>
    <head><title>Array Example</title>

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

    function count()
    {
    a = document.theform.textareaform.value;

    alert("You have " + a.length + " number of characters");
    }

    function countchars()
    {
    a = document.theform.textareaform.value;
    b = document.theform.textboxform.value;

    for(c = 0; c < a.length; c++)
    {
    if(b != a.length)
    {
    alert("The letter " + "'" + b + "'" + " does not appear in the text above");
    }
    else
    {
    alert("The letter " + "'" + b + "'" + " appears " + c + " many times in the text above");
    }
    }
    }

    -->
    </script>

    </head>
    <body>

    <form action="" method="post" name="theform">

    <textarea cols=40 name="textareaform" rows=10 wrap="hard"></textarea><br/>
    <input type="submit" onclick="count()" value="How many characters?">
    <input type="reset" value="Clear Form"><br/><br/>

    Type a letter: <input maxlength=1 name="textboxform" size=3 type="text"><br/>
    <input type="submit" onclick="countchars()" value="How many of each character?">
    <input type="reset" value="Clear Form">

    </form>

    </body>
    </html>

    I know my for loop is wrong, I am just stuck at this point lol even though I know how to do it in my head, but that doesn't mean anything if I can't put it into code :( Thanks in advance!
     
  2. Aimless

    Aimless Resident drunkey

    Joined:
    Nov 5, 2001
    Messages:
    2,534
    Likes Received:
    0
    Location:
    Wisconsin
    Code:
    <html>
    <head><title>Array Example</title>
    
    <script language="javascript" type="text/javascript">
    <!--
    
    function count()
    {
    var a = document.theform.textareaform.value;
    
    alert("You have " + a.length + " characters");
    }
    
    function countchars()
    {
    var a = document.theform.textareaform.value;
    var b = document.theform.textboxform.value;
    var count = 0;
    
    for(var c = 0; c < a.length; c++)
    {
    if(a[c] == b)
    {
    count++;
    }
    }
    if(count == 0)
    {
    alert("The letter " + b + " does not appear in the text above");
    }
    else
    {
     alert("The letter " + b + " appears " + count + " times in the text above");
    }
    }
    
    -->
    </script>
    
    </head>
    <body>
    
    <form action="" method="post" name="theform">
    
    <textarea cols=40 name="textareaform" rows=10 wrap="hard"></textarea><br/>
    <input type="submit" onclick="count()" value="How many characters?">
    <input type="reset" value="Clear Form"><br/><br/>
    
    Type a letter: <input maxlength=1 name="textboxform" size=3 type="text"><br/>
    <input type="submit" onclick="countchars()" value="How many of each character?">
    <input type="reset" value="Clear Form">
    
    </form>
    
    </body>
    </html>
    For an array example, you should probably make use of an array somewhere.
     
  3. z06

    z06 New Member

    Joined:
    May 27, 2003
    Messages:
    141
    Likes Received:
    0
    thanks buddy!! you're awesome! this wasn't suppose to be an array example though, i just forgot to change the title from the last program i made - thanks again!!
     

Share This Page