Write a JavaScript code to calculate the smallest number the

Write a JavaScript code to calculate the smallest number, the largest number, the sum and average of numbers in an array that has user input. A window prompt should be used to take in the size of the array as well as the inputted elements.

Solution

The program is as follows:

<!DOCTYPE html>
<html>
<body>

<p id=\"demo\"></p>

<script>
var l= prompt(\"Please enter length\");

var arr;
arr = prompt(\"Enter next number\").split(\",\");
document.getElementById(\"demo\").innerHTML = \"Max number is= \"+ Math.max.apply(null, arr)+ \" Min number is= \"+ Math.min.apply(null, arr)+\"Sum is=\" + sumArray(arr) + \"Average is =\" + sumArray(arr)/l;   

function sumArray(arr) {

for (var i = 0, sum = 0; i < arr.length; sum = +sum + +arr[i++])
;
return sum;
}

</script>

</body>

</html>

Write a JavaScript code to calculate the smallest number, the largest number, the sum and average of numbers in an array that has user input. A window prompt sh

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site