Created
January 2, 2021 17:39
-
-
Save mdutt247/8f2bf7440dc0dadccc8127f517b75a82 to your computer and use it in GitHub Desktop.
Push random numbers in array and write a function to find max
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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