Q:

How to determine the highest and lowest numbers from an array that generates random numbers; JavaScript

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.

A:

First of all, the initial values for minimum and maximum are wrong:
var lowestNum = 0;
var highestNum = 0;

The minimum value should be initialised as +Inf and maximum value as -Inf.
var lowestNum = Infinity, highestNum = -Infinity;

Then, add a simple condition that updates the lowest and highest found so far.
if (array[i] < lowestNum) {
    lowestNum = array[i];
}
if (array[i] > highestNum) {
    highestNum = array[i];
}

At the end of your loop, the variables will represent the lowest and highest number in the whole array.

