JS Basics: Find the maximum and minimum values in a set of numbers, and where they are located

Source: Internet
Author: User

1         vararr = [0, 5,-3, 6, 2,-6, 10];2         //define a maximum value and a minimum value, assigning their index value to a fixed two variable3         varMaxValue = arr[0];4         varMinValue = arr[0];5         varMaxindex = 0;6         varMinindex = 0;7          for(vari = 1; i < arr.length; i++) {8             if(Arr[i] >maxValue) {9                 //Assign this element to the maximum value, and assign the value of his corresponding index to Maxindex .TenMaxValue =Arr[i]; OneMaxindex =i; A             } -             //if the elements in the array are smaller than the minimum values we define -             if(Arr[i] <minValue) { theMinValue =Arr[i]; -Minindex =i; -             } -         } + Console.log (maxValue); - Console.log (maxindex); + Console.log (minValue); AConsole.log (Minindex);

JS Basics: Find the maximum and minimum values in a set of numbers, and where they are located

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.