Skip to main content
added 77 characters in body
Source Link
jfriend00
  • 711k
  • 104
  • 1.1k
  • 1k
function largestOfFour(master) {
    var result = [];
    // iterate through all arrays passed
    for (var i = 0; i < master.length; i++) {
        // master[i] is an array and can be just treated like any array
        result.push(Math.max.apply(Math, master[i]));
    }
    return result;
}

var r = largestOfFour(  [[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

// show result in snippet
document.write(JSON.stringify(r));
function largestOfFour(master) {
    var result = [];
    // iterate through all arrays passed
    for (var i = 0; i < master.length; i++) {
        result.push(Math.max.apply(Math, master[i]));
    }
    return result;
}

var r = largestOfFour(  [[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

// show result in snippet
document.write(JSON.stringify(r));
function largestOfFour(master) {
    var result = [];
    // iterate through all arrays passed
    for (var i = 0; i < master.length; i++) {
        // master[i] is an array and can be just treated like any array
        result.push(Math.max.apply(Math, master[i]));
    }
    return result;
}

var r = largestOfFour(  [[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

// show result in snippet
document.write(JSON.stringify(r));
added 1792 characters in body
Source Link
jfriend00
  • 711k
  • 104
  • 1.1k
  • 1k

For the second problem where you want to collect the largest number from each sub-array, you can do this (working snippet):

function largestOfFour(master) {
    var result = [];
    // iterate through all arrays passed
    for (var i = 0; i < master.length; i++) {
        result.push(Math.max.apply(Math, master[i]));
    }
    return result;
}

var r = largestOfFour(  [[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

// show result in snippet
document.write(JSON.stringify(r));


To explain a bit, Math.max.apply(Math, array) is a trick for finding the largest value in an array. It works like this:

Math.max() accepts as many arguments as you want to pass it. For example, you can do Math.max(1,2,3,4,5,6) and it will return 6. So, if you could pass it a whole array of arguments, then it would find the max value in the whole array.

So, how do you turn an array of values into a set of arguments to a function. Well, you can use .apply() to do that. It's a method on any function in Javascript. You can read about here on MDN. So, since Math.max() is a function, we can use Math.max.apply() to use an array of values as the arguments for Math.max(). .apply() accepts two arguments itself. The first is the this value that you want the function to have. That, it turns out is not really relevant here so we pass Math which just gives the .max() function the same this value that it would have if we called it as Math.max(). The second argument to .apply() is an array of values that we want to be the arguments to our function. For that, we just pass our array. So, we end up with:

Math.max.apply(Math, myArray);

to find the largest value in myArray. To see how this works, let's suppose that we have:

var myArray = [9,8,7,1];
var highest = Math.max.apply(Math, myArray);

That is the same as this:

var highest = Math.max(9,8,7,1);

The Math.max.apply(Math, myArray) takes the array of values in myArray and passes them as consecutive arguments to Math.max() just as if we had typed them into our code manually as arguments.

And, in both cases above, highest === 9.

For the second problem where you want to collect the largest number from each sub-array, you can do this (working snippet):

function largestOfFour(master) {
    var result = [];
    // iterate through all arrays passed
    for (var i = 0; i < master.length; i++) {
        result.push(Math.max.apply(Math, master[i]));
    }
    return result;
}

var r = largestOfFour(  [[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

// show result in snippet
document.write(JSON.stringify(r));

For the second problem where you want to collect the largest number from each sub-array, you can do this (working snippet):

function largestOfFour(master) {
    var result = [];
    // iterate through all arrays passed
    for (var i = 0; i < master.length; i++) {
        result.push(Math.max.apply(Math, master[i]));
    }
    return result;
}

var r = largestOfFour(  [[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

// show result in snippet
document.write(JSON.stringify(r));


To explain a bit, Math.max.apply(Math, array) is a trick for finding the largest value in an array. It works like this:

Math.max() accepts as many arguments as you want to pass it. For example, you can do Math.max(1,2,3,4,5,6) and it will return 6. So, if you could pass it a whole array of arguments, then it would find the max value in the whole array.

So, how do you turn an array of values into a set of arguments to a function. Well, you can use .apply() to do that. It's a method on any function in Javascript. You can read about here on MDN. So, since Math.max() is a function, we can use Math.max.apply() to use an array of values as the arguments for Math.max(). .apply() accepts two arguments itself. The first is the this value that you want the function to have. That, it turns out is not really relevant here so we pass Math which just gives the .max() function the same this value that it would have if we called it as Math.max(). The second argument to .apply() is an array of values that we want to be the arguments to our function. For that, we just pass our array. So, we end up with:

Math.max.apply(Math, myArray);

to find the largest value in myArray. To see how this works, let's suppose that we have:

var myArray = [9,8,7,1];
var highest = Math.max.apply(Math, myArray);

That is the same as this:

var highest = Math.max(9,8,7,1);

The Math.max.apply(Math, myArray) takes the array of values in myArray and passes them as consecutive arguments to Math.max() just as if we had typed them into our code manually as arguments.

And, in both cases above, highest === 9.

Source Link
jfriend00
  • 711k
  • 104
  • 1.1k
  • 1k

For the second problem where you want to collect the largest number from each sub-array, you can do this (working snippet):

function largestOfFour(master) {
    var result = [];
    // iterate through all arrays passed
    for (var i = 0; i < master.length; i++) {
        result.push(Math.max.apply(Math, master[i]));
    }
    return result;
}

var r = largestOfFour(  [[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

// show result in snippet
document.write(JSON.stringify(r));