Skip to content

Instantly share code, notes, and snippets.

@mdutt247
Created January 2, 2021 17:39
Show Gist options
  • Save mdutt247/8f2bf7440dc0dadccc8127f517b75a82 to your computer and use it in GitHub Desktop.
Save mdutt247/8f2bf7440dc0dadccc8127f517b75a82 to your computer and use it in GitHub Desktop.
Push random numbers in array and write a function to find max
<!DOCTYPE html>
<html lang="en">
<head>
<title>Array random and max</title>
</head>
<body>
<h1>Array elements are</h1>
<div id="random"></div>
<h2>Maximum number is</h2>
<div id="max"></div>
<script>
function maxValue(arr) {
var max = arr[0];
for (var val of arr) {
if (val > max) {
max = val;
}
}
return max;
}
var arr = [];
for(var i=0;i<100;i++)
arr.push(Math.floor(Math.random() * 1000));
document.getElementById("random").innerHTML = arr;
var max = maxValue(arr);
document.getElementById("max").innerHTML = max;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment