1

I need to sort an array of numbers, so that it returns its minumum and its maximum. The code I've written so far doesn't seem to do anything at all.

function sort(array) {
    arrayNew = [];
    maximum = array[0];
    minimum = array[0];

    for (i = 0; i < array.length; i++) {
        if (maximum < array[i]) {
            maximum = array[i];
        }
    }
    arrayNew.push(maximum);

    for (i = 0; i < array.length; i++) {
        if (minimum > array[i]) {
            minimum = array[i];
        }
    }
    arrayNew.unshift(minumum);
    return arrayNew;
}


var arr1 = [3, 8, 7, 6, 5, -4, 3, 2, 1];
alert(sort(arr1));
1
  • You have a misspelling: arrayNew.unshift(minumum);
    – trnelson
    Commented Oct 22, 2014 at 10:26

4 Answers 4

2

In case you didn't know there is a much simpler way to get the min and max values from an array - use Math.min and Math.min with apply:

var min = Math.min.apply(null, arr1);
var max = Math.max.apply(null, arr1);

DEMO

2
  • Thanks for your reply, I knew about the Math object, I was just trying this as a "side" exercise of one I was assigned Commented Oct 22, 2014 at 20:58
  • Even I thought about this, but then I decided to go with the looping. Because, it finds the max and min in single iteration. Commented Oct 23, 2014 at 3:23
1
  1. minumum is spelled incorrectly. It should have been minimum

  2. Don't use your variables without declaring them with var keyword. Otherwise they will become global properties.

  3. Also, you can have your minimum check also in the same loop as maximum. So, your code can be shortened like this

    function sort(array) {
        var maximum = array[0],
            minimum = array[0],
            i;
    
        for (i = 0; i < array.length; i++) {
            if (maximum < array[i]) {
                maximum = array[i];
            }
    
            if (minimum > array[i]) {
                minimum = array[i];
            }
        }
        return [minimum, maximum];
    }
    
  4. Now that we know, we can do it like this, lets try to shorten it more with ternary operator

    function sort(array) {
        var maximum = array[0],
            minimum = array[0],
            i;
    
        for (i = 0; i < array.length; i++) {
            maximum = maximum < array[i] ? array[i] : maximum;
            minimum = minimum > array[i] ? array[i] : minimum;
        }
        return [minimum, maximum];
    }
    
1
  • Not for (var i = 0? particularly given your second point? @thefourtheye?
    – Andy
    Commented Oct 23, 2014 at 0:08
1
var arr = [ 3, 8, 7, 6, 5, -4, 31, 2, 21, 20, 1 ].sort(function(a, b) { return a - b }),  // [-4, 1, 2, 3, 5, 6, 7, 8, 20, 21, 31]
    min = arr[0],  // min
    max = arr[arr.length-1];  //max
2
  • javascript's default sort is lexiographic not numerical, that function won't work for numbers above 10. Yes the OP used the same array but they never said they are limited to this set. Best to correct it for posterity. Commented Oct 22, 2014 at 10:34
  • 1
    Updated the sort method with a sort function.
    – user235273
    Commented Oct 22, 2014 at 10:45
0

arrayNew.unshift(minumum); is wrong variable minimum

correct one : arrayNew.unshift(minimum);

2
  • Please double-check your answer. It does not make any sense.
    – PM 77-1
    Commented Oct 23, 2014 at 3:30
  • I checked my answer and he is using wrong variable. minumum does not exists. Commented Oct 23, 2014 at 5:58

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.