Write a function that determines the maximum / minimum of the array in javascript

[WITHOUT USING MIN / MATH Function]

Ask a question to create a function that includes 2 parameters. The first is an array, and the second parameter is the string " Minimum " or " Maximum ". This confuses me when I don't know how to enter a parameter as a string in a function. So I decided to create 2 similar functions, such as extremeValue(vector, maximum) and extremeValue(vector, minimum) (I still don't know if I can do this or not). And this is my code for extremeValue(vector, maximum) .

So, the way I did this is to create an array that has the same meaning with vector[i] that I use the if statement to see if it is larger than this vector or not. However, the code does not work. :(

 var vector = [3, 1, 1] var maximum //------------this function give us the vector with all same value---------------// function set(value, len) { var arr = []; for (var i = 0; i < len; i++) { arr.push(value); } return arr; } //---------------------------------------------------// //---------------------------------------------------// function extremeValue(vector, maximum) { var answer = ""; for (var count = 1; count++; count < vector.length) { if (set(vector[count], vector.length) > vector) answer += vector[count] + "is maximum" } return answer } //---------------------------------------------------// console.log(extremeValue(vector, maximum)) 
+4
source share
4 answers

Without using Math or any other javascript function, you can find the max and min values ​​of the array, as shown below.

 var arr = [2, 3, 5, 10, 2, -9, 3]; alert("Max value is " + arrayMaxMin(arr, "Max")); alert("Min value is " + arrayMaxMin(arr, "Min")); function arrayMaxMin(array, selector) { var val = ""; // variable to hold the current max/min value. for (var i = 0; i < array.length; i++) { if (selector == "Min") { if (array[i] < val) { val = arr[i]; } } else if (selector == "Max") { if (array[i] > val) { val = arr[i]; } } } return val; } 
+2
source

You can use the Math functions. Math.min will return you the least number of numbers passed. You cannot call these functions directly in an array, so you can use apply to call these functions and pass array parameters to functions.

The Math.min() function returns the smallest number from zero or more.

Math.max as follows:

 // array: The source array // type: 'max' or 'min' to find Maximum or Minimum number var getNum = function(array, type) { return Math[type].apply(null, array); }; var arr = [1, 3, 35, 12]; document.write('Max: ' + getNum(arr, 'max')); document.write('<br />Min: ' + getNum(arr, 'min')); 

UPDATE

they do not allow the use of the min max function

You can use array methods to get the maximum and minimum values. Sort the array in ascending order, and then the first element in the array will be minimum, and the last - maximum.

 // array: Source array // type: 'max' or 'min' var getNum = function(array, type) { // Sort the array by ascending order array.sort(function(a, b) { return a < b; }); return type === 'max' ? array[0] : array[array.length - 1]; }; var vector = [233, 10, 32543, 54321]; var max = getNum(vector, 'max'); var min = getNum(vector, 'min'); document.write('Max: ' + max); document.write('<br />Min: ' + min); 
+3
source

That should work too !. Hope this helps brother.

  var arr = [2 ,4 ,56, 23, 10]; var max = arr.reduce(function(x,y){ return (x > y) ? x : y; }); var min = arr.reduce(function(x,y){ return (x < y) ? x : y; }); console.log('Max: '+ max); console.log('Min: '+ min); 
+3
source

You can do something like this:

 function extremeValue(my_arr, min_max) { var retval, i, len; for(i=0,len=my_arr.length; i < len; i++) { if (min_max == 'minimum') { retval = (my_arr[i] < retval ? my_arr[i] : retval); } else { retval = (my_arr[i] > retval ? my_arr[i] : retval); } } return retval; } 

Then you would call it like this:

 console.log(extremeValue(vector, 'maximum')); console.log(extremeValue(vector, 'minimum')); 
+2
source

All Articles