Saturday, 23 April 2016

Create A Array Using JAVASCRIPT That Calculate A Sum Average Smallest And Largest Element In Array


<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>Array Processing</title>

<script>

function doProcess() {
var fields=document.forms[0].getElementsByTagName('INPUT');
var val=0;
var min=0;
var max=0;
var total=0;
for (var i=0;i<fields.length;i++) {

if ((fields[i].type=='text')&&(fields[i].name.indexOf('number')==0)) {
val=parseInt(fields[i].value);
if ((val<0)||(val>100)) {
alert('only numbers between 0 and 100!');

return false;
}

if (isNaN(val)) {
alert('All fields have to contain numbers');
return false;
}
total+=val*1;
if (val>max) max=val;
if (val<min) min=val;
}

}
document.forms[0].sum.value = total;
document.forms[0].average.value = total / 10;
document.forms[0].smallest.value = min;
document.forms[0].largest.value = max;
}

</script>
</head>
<body align="center">
<h1>Array Processing</h1> <br>

<h3>Enter The Number Between 0 To 100 </h3> <br>

<form method = "post" action = "/cgi-bin/formmail" >


<input name = "number0" type = "text" size = "4"
maxlength = "5" />


<input name = "number1" type = "text" size = "4"
maxlength = "5" />


<input name = "number2" type = "text" size = "4"
maxlength = "5" />


<input name = "number3" type = "text" size = "4"
maxlength = "5" />


<input name = "number4" type = "text" size = "4"
maxlength = "5" />


<input name = "number5" type = "text" size = "4"
maxlength = "5" />


<input name = "number6" type = "text" size = "4"
maxlength = "5" />


<input name = "number7" type = "text" size = "4"
maxlength = "5" />


<input name = "number8" type = "text" size = "4"
maxlength = "5" />


<input name = "number9" type = "text" size = "4"
maxlength = "5" />

<br><br>

<input type="button" name="click1" value="Process" onClick="doProcess()">


<p>
<label>Sum:
<input name = "sum" type = "text" size = "4"
maxlength = "10" />


<label>Average:
<input name = "average" type = "text" size = "4"
maxlength = "10" />

<label>Smallest:
<input name = "smallest" type = "text" size = "4"
maxlength = "10" />

<label>Largest:
<input name = "largest" type = "text" size = "4"
maxlength = "10" />
</form>

</body>
</html>

Output:-



www.programmingwithbasics.com

No comments:
Write comments

Recommended Posts × +