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 6 of 6
  1. #1
    CAO
    CAO is offline
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do you parse a split() textarea?

    Hey
    I'm trying to create an aggregates calculator where the sum & average are calculated from the user inputs textarea of values, which i have split(). This is my code so far. I'm sure I'm not using the variables or parseFloat method correctly.
    Any advise?
    Many Thanks


    <html>
    <head>
    <script type="text/javascript">
    <!--

    function add()
    {
    document.getElementById("answer").value+=(document.getElementById("num1").value) + '\n';
    }


    function calculate()
    {
    var total = "sa";
    var sa = textAreaText.split("\n");
    for (var i=0; i < sa.length; i++)
    parseFloat(sa[i]) + total;
    {
    document.getElementById("sum").value = total;
    }
    {
    document.getElementById("average").value = total / sa.length;
    }
    }



    //-->
    </script>
    </head>
    <body>
    <h1>Aggregates</h1>

    <h3>Add as many numbers as you like<br>to the list,then click Calculate.</h3>

    <form name="entryForm" id="entryForm">

    <input type="text" id="num1"></input>

    <input type="button" value="Add to list" onclick="add();"><br>

    <textarea name="text" rows="15" cols="20" readonly="readonly" id="answer"></textarea><br>

    <input type="button" value="Calculate" onclick="calculate">

    <input type="reset" value="Reset">

    <p>Total (Sum);</p>

    <input type="text" id="sum" value="0"><br>

    <p>Average :</p>

    <input type="text" id="average" value="0">

    </form>

    </body>
    </html>

  • #2
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    1) You have syntax and logic errors, but overall you are reasonably close.

    2) In the future, posted code is much easier to read if you wrap the code in code tags by first highlighting your code and then click the # icon in the editor's toolbar.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <script type="text/javascript">
                function add(){
                    document.getElementById("answer").value+=(document.getElementById("num1").value) + "\n";
                    document.getElementById("num1").value = '';
                    document.getElementById("num1").focus();
                }
                function calculate(){
                    var total = 0, numCount = 0;
                    var sa = document.getElementById('answer').value.split("\n"); 
                    for (var i=0; i < sa.length; i++) {
                        if(!isNaN(parseFloat(sa[i]))){
                            total += parseFloat(sa[i]);
                            ++numCount;
                        }
                    }
                        document.getElementById("sum").value = total;
                        document.getElementById("average").value = total / numCount;
                }
            </script>
        </head>
        <body>
            <h1>Aggregates</h1>
    
            <h3>Add as many numbers as you like<br>to the list,then click Calculate.</h3>
    
            <form name="entryForm" id="entryForm">
    
                <input type="text" id="num1" />
    
                <input type="button" value="Add to list" onclick="add();"><br />
    
                    <textarea rows="15" cols="20" readonly="readonly" id="answer"></textarea><br />
    
                    <input type="button" value="Calculate" onclick="calculate();" />
    
                    <input type="reset" value="Reset" />
    
                    <p>Total (Sum);</p>
    
                    <input type="text" id="sum" value="0" /><br />
    
                    <p>Average :</p>
    
                    <input type="text" id="average" value="0" />
    
            </form>
        </body>
    </html>
    Last edited by webdev1958; 03-08-2012 at 03:57 AM.

  • #3
    CAO
    CAO is offline
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    webdev1958 THANK YOU!

    I've spent about 40 hours trying to figure this out and will now spend another 40 studying your code until I completely understand it.

    Much appreciated.

  • #4
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by CAO View Post
    webdev1958 THANK YOU!
    glad to have helped. Google should help explain anthing you're not sure of, but if you need more help or explanation, post back

  • #5
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Do you by chance have perl CGI version to do the same thing? I was struggling with this in perl CGI. Thank you in advance!
    Tany

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,021
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Post in the PERL forum.

    But it will be *nothing* like the JavaScript version.

    It will have to process SUBMITTED data, no data that is live in the browser.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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