Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-08-2012, 02:42 AM   PM User | #1
CAO
New to the CF scene

 
Join Date: Mar 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
CAO is an unknown quantity at this point
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>
CAO is offline   Reply With Quote
Old 03-08-2012, 03:25 AM   PM User | #2
webdev1958
Banned

 
Join Date: Apr 2011
Posts: 656
Thanks: 14
Thanked 69 Times in 69 Posts
webdev1958 can only hope to improve
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..
webdev1958 is offline   Reply With Quote
Old 03-08-2012, 04:08 AM   PM User | #3
CAO
New to the CF scene

 
Join Date: Mar 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
CAO is an unknown quantity at this point
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.
CAO is offline   Reply With Quote
Old 03-08-2012, 10:05 AM   PM User | #4
webdev1958
Banned

 
Join Date: Apr 2011
Posts: 656
Thanks: 14
Thanked 69 Times in 69 Posts
webdev1958 can only hope to improve
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
webdev1958 is offline   Reply With Quote
Old 11-17-2012, 12:52 AM   PM User | #5
yifangt
New to the CF scene

 
Join Date: Feb 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
yifangt is an unknown quantity at this point
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
yifangt is offline   Reply With Quote
Old 11-17-2012, 01:11 AM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:04 AM.


Advertisement
Log in to turn off these ads.