Home  • Programming • JavaScript

JavaScript program to find smallest number, largest number and sum up array of numbers

Code Solutions [4384] Problem Definition Find the smallest number, largest number and sum up all the numbers from the following array: var n=[4,5,67,3,45,74] Code
<script>
 var n=[4,5,67,3,45,74];
 var smallest=n[0];
 var largest= n[0];
 var sum=0;
 for(i=0;i<n.length;i++){
     if(smallest>n[i]){
     smallest=n[i];
     }
	if(largest<n[i]){
	   largest=n[i];
	}
    sum+=n[i];
}
document.write("Smallest:"+smallest+"<br/>");
document.write("Largest:"+largest+"<br/>");
document.write("Sum:"+sum+"<br/>");
</script>
Output Smallest:3 Largest:74 Sum:198

Comments 0


Copyright © 2024. Powered by Intellect Software Ltd