Use JavaScript programming to obtain the maximum value among a set of random numbers

Source: Internet
Author: User

To use JavaScript programming to obtain the maximum value among a set of random numbers, the first is the simplest:

 var a =[];
var counter = 50;
for(var i=0;i<counter;i++)
{
a[i] = Math.ceil(1000*Math.random());
}

for(var j=0;j<counter;j++)
{
if(a[0]<a[j])
    {
        tempNum = a[0];
        a[0] = a[j];
        a[j] = tempNum;
    }
    else
    {
        tempNum = a[0];
    }   
}
var maxNum = tempNum;

console.log("the max number is: " + maxNum);

In the code above, the second for loop filters (filters) The values in array ). If the condition of a [0] <A [J] is true (true), it will be filtered in; otherwise, it will reach the else {}, which is in the first subscript (0) when the value of is greater than any value after it.

Next we will use the Javascript native function to sort the array elements in sequence (sort () method), and then pop () the last element of the array:

 var a =[];
for(var i=0;i<20;i++)
{
a[i] = Math.ceil(1000*Math.random());
}
console.log(a);
var sortArr = a.sort();

var maxNum = sortArr.pop();
console.log("the max number is: " + maxNum);

It can be observed that the sort () method is compared from the first (first), not from the first position. Then compare the second digit with the third digit until the last digit (the last digit ). This sorting is from small to large. Pop () returns the last value of the array. This is what we want. Note that the POP () method deletes the last array element, so the length of array a is-1. If you don't believe it, you can use a. length to check it.

The sort () method of the array in the sample code above sorts the numbers in the array.Bubble Sorting AlgorithmIn the ascending order or in ascending order:

 var a = [];
for(var i=0;i<20;i++)
{
a[i] = Math.ceil(1000*Math.random());
}
var length = a.length;
for(var i=0;i<length-2;i++)
{
for(var j=length-1;j>=1;j--)
{
if(a[j]<a[j-1])
{
temp = a[j]; a[j] = a[j-1]; a[j-1] = temp;
}
}
}
console.log(a);
var maxNum = a[a.length - 1];
console.log(maxNum);
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.