Write a program to find the min number of an array in JavaScript?

find the min number of an array


1 Using Loop

  

        function minNumber(numberArray){

        var arrayLength = numberArray.length;
        var min = Infinity;
        console.log(min);
        var i;
        for (i = 0; arrayLength > i; i++) {
        if (numberArray[i] < min) {
        console.log(i + ' min number' + numberArray[i]);
        min = numberArray[i];
        }
        }
        return min;

        }

        const array = [37,-5,-40,99243,5,15]
        const Numbers = [1, 2, 101, 45, 55, 1443];

        var result = minNumber(Numbers);
        var result1 = minNumber(array);

        console.log(result);
        console.log(result1);
    

2. Using the spread operator

  

        const array = [37,-5,-40,99243,5,15]
        const Numbers = [1, 2, 101, 45, 55, 1443];
        var result1= Math.min(...array);
        var result2= Math.min(...Numbers);

        console.log(result1);
        console.log(result2);

    

3. Using Function.prototype.apply()

  

        const array = [37,-5,-40,99243,5,15]
        const Numbers = [1, 2, 101, 45, 55, 1443];
        var result3= Math.min.apply(null,array);
        var result4= Math.min.apply(null,Numbers);

        console.log(result3);
        console.log(result4);

    

4. using math.min() function

  

        var result1= Math.min(37,-5,-40,99243,5,15);
        var result2= Math.min(1, 2, 101, 45, 55, 1443);
        console.log(result1);
        console.log(result2);

    

5. using sorting

  

        const array = [10,-5,-10,99243,5,30]
        array.sort();
        var result1 = array[0];
        console.log(result1);

    

6. using reduce

  

        const array = [10,-5,-10,99243,5,30]

        var result = array.reduce(function(x,y){
        return Math.min(x,y)
        })

        console.log(result);
    

7. MDN hybrid solution

  

        function minOfArray(arr) {
        var min = Infinity;
        var QUANTUM = 32768;

        for (var i = 0, len = arr.length; i < len; i += QUANTUM) {
        var submin = Math.min.apply(null, arr.slice(i, Math.min(i + QUANTUM, len)));
        min = Math.min(submin, min);
        }

        return min;
        }

        const array = [10,-5,-10,99243,5,30]

        var result = minOfArray(array);
        console.log(result);

    

8. Function.prototype.apply.bind

  

        const array = [10,-5,-10,99243,5,30]
        var arrayMax = Function.prototype.apply.bind(Math.min, null);
        var result = arrayMax(array);
        console.log(result);

    

9. Using lambda expression

  

        const array = [10,-5,-10,99243,5,30]

        let min= x=> x.reduce((m,n)=> m<n ? m:n);
                                          var result=min(array);
                                          console.log(result);