3

[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))
0

5 Answers 5

4

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

Sign up to request clarification or add additional context in comments.

4 Comments

Hi. they does not allow us use min max function :(
@TimNguyen I have updated the answer to make it work without using Math functions :)
arrayMaxMin([10,1,12], "Min") won't work. Your example works only because you have negative number in it. 1 < '' will return false. You can change your answer to var val = array[0] and for (var i = 1; i < array.length; i++) {
Also val = arr[i]; should be to val = array[i]
3

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

1 Comment

Hi. they does not allow us use min max function :(
3

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

1 Comment

MAX is not a constant so you need to use max as your naming convention?
2

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

2 Comments

Hi. they does not allow us use min max function :(
I didn't use Math.min() or Math.max().
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.