Javascript:
var arrayLength = prompt("Enter how many elements you want?");
if(isNaN(arrayLength) || arrayLength < 1){
arrayLength = 50;
}
var array = [];
var list = "<ul>";
var totalSum = 0;
var totalOddSum = 0;
var totalEvenSum = 0;
var lowestNum = 0;
var highestNum = 0;
for(i = 1; i <= arrayLength; i++){
array[i] = parseInt(Math.random() * 15);
list += "<li>" + array[i] + "</li>";
totalSum += array[i];
if(array[i] % 2 == 0){
totalEvenSum += array[i];
}
else{
totalOddSum += array[i];
}
}
list += "</ul>";
document.getElementById("div1").innerHTML += list;
document.getElementById("div2").innerHTML += "Total Sum: " + totalSum + "<br>";
document.getElementById("div2").innerHTML += "Total Even Sum: " + totalEvenSum + "<br>";
document.getElementById("div2").innerHTML += "Total Odd Sum: " + totalOddSum + "<br>";
document.getElementById("div2").innerHTML += "Lowest Number: " + lowestNum + "<br>";
document.getElementById("div2").innerHTML += "Highest Number: " + highestNum + "<br>";
HTML:
<div id="div1">
</div>
<div id="div2">
</div>
As the title says, I want to be able to able to find the lowest and highest numbers that my array generates. What do I need to implement inside my loop in order to find the highest and lowest numbers Math.random() generates?
Thanks guys.
Aucun commentaire:
Enregistrer un commentaire