# Javascript Program to Find the Sum, Average, Smallest and Largest Number of an Array.

Create an array using javascript that calculates a sum, average, smallest, and largest element in the array. So basically array has fixed size and we have to find the only sum of all the elements, an average of all the numbers, smallest number of an array and the largest number of an array. there is 4 task to solve this problem. As we know that for finding a sum of all the number we have to traverse zero indexes to the last index of an array and add all those numbers and same procedure for find average of a number only difference is that we have to divide the sum by number of elements, and for smallest and largest number we have to compare a number with all existing number of an array and in the number is small compared to the other number that we get a smallest elements or number of an array if the number is greater than we get the largest elements of number of an array.

If you find any difficulty to please comment below.

### Javascript code for find Sum, Average, Smallest and largest Number.

<html xmlns = "http://www.w3.org/1999/xhtml">
<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>

<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:-

Previous Post
Next Post

#### post written by: Ghanendra Yadav

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.