Tim Nguyen
Tim Nguyen

Reputation: 85

write a function finding maximum/minimum of array in javascript

[WITHOUT USING MIN/MATH Function]

The question ask me to create the function including 2 parameters.first is an array and the second parameter is either the String “Minimum” or “Maximum” . It confusing me when i don't know how to input the parameter as the string in the function. So that i decide to create 2 similar function as extremeValue(vector, maximum) and extremeValue(vector, minimum) ( I still don't know whether i can do that or not ). And this is my code for extremeValue(vector, maximum).

So the way i did is create the array which have the same value with vector[i] than i using if statement to see if it bigger than that vector or not. However, The code is doesn't 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))

Upvotes: 3

Views: 10246

Answers (5)

afsarkhan10182
afsarkhan10182

Reputation: 2212

Without the Built in Math Min/Max method:

let minMaxValues = (arr) => {
    let maxValues;
    let minValues;
 
    for (let i = 0; i < arr.length; i++) {
      // check first value with all iterater
      if(arr[i] < arr[1]){
        minValues = arr[i];
      }

      // check last value with all iterater
      if(arr[i] > arr[arr.length-1]){
        maxValues = arr[i];
      }
      
    }
    console.log(minValues);
    console.log(maxValues);
  }

  minMaxValues([100, 20, 30, 10, 50]);

Upvotes: 0

Aruna Tebel
Aruna Tebel

Reputation: 1466

Without using Math or any other javascript function, you can find the max and min values of an array as 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 = array[0];   // variable to hold the current max/min value.

  for (var i = 1; i < array.length; i++) {
    if (selector == "Min") {
      if (array[i] < val) {
        val = array[i];
      }
    } else if (selector == "Max") {
      if (array[i] > val) {
        val = array[i];
      }
    }

  }
  return val;
}

Upvotes: 4

HenryDev
HenryDev

Reputation: 4993

This should work too!. Hope it helps bro.

 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);

Upvotes: 3

Karl Wilbur
Karl Wilbur

Reputation: 6217

You could 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'));

Upvotes: 2

Tushar
Tushar

Reputation: 87233

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

The Math.min() function returns the smallest of zero or more numbers.

Math.max as follow:

// 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 does not allow us use min max function

You can use array methods to get maximum and minimum value. Sort the array in ascending order and then the first element in array will be minimum and last element is 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);

Upvotes: 3

Related Questions