Min and max values in an array in javascript assignment expert

Min & Max Values in Array

given an array myArray of integers, write a JS program to find the maximum and minimum values in the array.

input1

[1,4,2,7,9,3,5]

output1

{min: 1, max: 9}

"use strict";

process.stdin.resume[];

process.stdin.setEncoding["utf-8"];

let inputString = "";

let currentLine = 0;

process.stdin.on["data", [inputStdin] => {

 inputString += inputStdin;

}];

process.stdin.on["end", [_] => {

 inputString = inputString

  .trim[]

  .split["\n"]

  .map[[str] => str.trim[]];

 main[];

}];

function readLine[] {

 return inputString[currentLine++];

}

/* Please do not modify anything above this line */

function findMinAndMaxValuesInArray[arr] {

 /* Write your code here */

}

/* Please do not modify anything below this line */

function main[] {

 let myArray = JSON.parse[readLine[]];

 console.log[findMinAndMaxValuesInArray[myArray]];

}

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The minimum and maximum element in an array can be found using 2 approaches:

    Method 1: Using Math.min[] and Math.max[]
    The min[] and max[] methods of the Math object are static functions that return the minimum and maximum element passed to it. These functions could be passed an array with the spread[…] operator. The spread operator allows an iterable to expand in places where multiple arguments are expected. In this case, it automatically expands array and gives the numbers to the functions.

    Syntax:

    minValue = Math.min[...array];
    maxValue = Math.max[...array];

    Example 1:

        

          Find the min/max element

          of an Array using JavaScript

      

        

          GeeksforGeeks

      

        Find the min/max element of 

          an Array using JavaScript

        Click on the button below t

          o find out the minimum and

          maximum of the array 

          [50, 60, 20, 10, 40]

        Minimum element is:

          

            Maximum Element is:

          

        

          Click to check

      

        

            function findMinMax[] {

                array = [50, 60, 20, 10, 40];

                minValue = Math.min[...array];

                maxValue = Math.max[...array];

                document.querySelector[

                  '.min'].textContent = minValue;

                document.querySelector[

                  '.max'].textContent = maxValue;

            }

        

    Output:

    Method 2: Iterating through the array and keeping track of the minimum and maximum element

    The minimum and maximum element can be kept track by iterating through all the elements in the array and updating the minimum and maximum element upto that point by comparing them to the current minimum and maximum values. Initially, the minimum and maximum values are initialized to Infinity and -Infinity.

    Syntax:

    minValue = Infinity;
    maxValue = -Infinity;
    
    for [item of array] {
        // find minimum value
        if [item < minValue]
        minValue = item;
                    
        // find maximum value
        if [item > maxValue]
        maxValue = item;
    }

    Example:

        

          Find the min/max element 

          of an Array using JavaScript

      

        

          GeeksforGeeks

      

        

          Find the min/max element

          of an Array using JavaScript

      

        

          Click on the button below to

          find out the minimum and 

          maximum of the array 

          [50, 60, 20, 10, 40]

      

        Minimum element is:

          

            Maximum Element is:

          

        

          Click to check

      

        

            function findMinMax[] {

                array = [50, 60, 20, 10, 40];

                minValue = Infinity;

                maxValue = -Infinity;

                for [item of array] {

                    // find minimum value

                    if [item < minValue]

                        minValue = item;

                    // find maximum value

                    if [item > maxValue]

                        maxValue = item;

                }

                document.querySelector[

                  '.min'].textContent = minValue;

                document.querySelector[

                  '.max'].textContent = maxValue;

            }

        

    Output:


    How to find min and max value in array in JavaScript?

    Array.prototype.reduce[] can be used to find the maximum element in a numeric array, by comparing each value:.
    const arr = [1, 2, 3]; const max = arr. reduce[[a, b] => Math. max[a, b], -Infinity]; ... .
    function getMaxOfArray[numArray] { return Math. max. ... .
    const arr = [1, 2, 3]; const max = Math. max[....

    How to find min element in array JavaScript?

    To get the index of the min value in an array:.
    Use the Math. min[] method to get the min value in the array..
    Use the Array. indexOf[] method to get the index of the min value..
    The indexOf method returns the index of the first occurrence of the value in the array..

    How do you find the highest and lowest numbers in JavaScript?

    To get the highest or lowest number from an array in JavaScript, you can use the Math. max[] or the Math. min[] methods then spread the elements from the array to these methods using the spread operator [ ... ]. Both the methods Math.

    Chủ Đề