View Single Post
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