Jump to content

How to return a value to a function in Javasript


khavson
 Share

Recommended Posts

/* write a javascript program to accept 10 numbers from  a user and return the average in an appropraite	statement. The numbers shouldbe enterd one at a time. Allow the user to use refresh button 	to run script again */function Avg(avg){	var num=[],i,avg;		var sum=0;		alert("Enter 10 numbers:");      		for(i=0; i<10; i++)		{				num[i]=prompt("Enter"+" "+(i+1)+" "+"number:");		}				for(i=0; i<10; i++)		{			sum += parseInt(num[i]);				avg=sum/parseInt(10);		}				document.writeln("Average is:"+" "+avg);			}function reloadPage(){location.reload();}
Link to comment
Share on other sites

Is that a question? Yes, there are three popups that are available in Javascript, but generally they are only used for debug purposes... http://www.w3schools.com/js/js_popup.asp

 

Mostly you want to provide a user interface on the page...

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>avg</title><style>.long{ width:400px;}</style><script>window.onload = init;function init(){document.getElementById('btn1').onclick = Avg;}function Avg(){var str = document.getElementById('in1').value;var arr = null;var cnt = 0;var sum = 0;//alert('['+str+']');if(str.length==0){}else if(str.indexOf(',')!=-1){var arr = str.split(',');}else if(str.indexOf(' ')!=-1){var arr = str.split(' ');}if (arr!=null){for(var i=0 ; i<arr.length ; i++){if(!isNaN(arr[i])){sum += parseFloat(arr[i]);cnt++;}document.getElementById('out1').innerHTML = "Average of "+ cnt +" values is: "+ (sum/cnt);}}}		</script></head><body><h3>Enter a list of numbers:</h3><input type="text" id="in1" class="long"/><br/><input type="button" id="btn1" value="Average"/><br/><br/><div id="out1"></div></body>    </html>
Link to comment
Share on other sites

here is an expandable script which calculates a variable amount of numbers you providehowever , I don't think this is quite the answer you might be looking for (not sure if you even had a question; you weren't exactly clear).

<!DOCTYPE html><html>    <head>       <script>            var list,avg;            window.onload = function (){                list = document.getElementById("list");                avg = document.getElementById("average");                                //start with 10 default blanks                for (var i=0;i<10;i++)                     add();            }                //allows you to set as many numbers as you want.             //builds a textfield and a remove button to go with it            function add(){                var input, div = document.createElement("div");                list.appendChild(div);                                input = document.createElement("input");                input.type = "text";                input.onkeyup = calc;                div.appendChild(input);                input.focus();                                input = document.createElement("input");                input.type = "button";                input.value="Remove"                input.onclick = function(){                    this.parentElement.remove();                    return calc();                };                div.appendChild(input);                                return calc();            }            //removes all numbers            function reset(){                while(list.firstElementChild != null)                    list.firstElementChild.remove();                return calc();            }                        //finds the average of only the numbers it can parse            function calc(){                var iter = list.firstElementChild;                var sum = 0;                var count = 0;                while(iter !=null){                    var val = parseFloat(iter.firstElementChild.value)                    if(!isNaN(val)){                        sum += val;                        count++;                    }                    iter = iter.nextElementSibling;                }                avg.innerHTML = isNaN(sum/count)                                    ?""                                    :sum/count;            }        </script>            </head>    <body>            <div id="list"></div>        <input type="button" value="add" onclick="add()">        <input type="button" value="reset" onclick="reset()">        <div>Average:<span id="average"></span></div>                </body></html>
Edited by Hadien
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...