1

I am trying to find the maximum number within each of four subarrays, push each max to a new array that includes all four maxes and return that array. When I run this code it returns an array with four values, but they are all null. What am I doing wrong?

largest = [];

function largestOfFour(arr) {
  for (i = 0; arr.length > i; i++) {
    var tempSub = arr.slice(i , i + 1);
    largest.push(Math.max.apply(Math, tempSub));
  } 
  return largest;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]], "");
6
  • 3
    What is your expected output? [ 5, 27, 39, 1001 ]? Commented Sep 17, 2015 at 17:18
  • 2
    Why are you passing 2 parameters to largestOfFour()? Commented Sep 17, 2015 at 17:19
  • @Halcyon Yes, thats what I am looking for Commented Sep 17, 2015 at 17:22
  • @RocketHazmat The second parameter is for a test I believe on freecodecamp. I tried running without it but nothing changed. Commented Sep 17, 2015 at 17:25
  • That's because largestOfFour(arr) only lists one parameter in its signature. Commented Sep 17, 2015 at 17:27

2 Answers 2

3

When you are using slice to get part of the array, you are getting an array with the item inside it. The variable tempSub doesn't contain an array of values like [4, 5, 1, 3], it contains an array of arrays of values, like [[4, 5, 1, 3]]. When you send that into the max method it will not be able to get the maximum value because you don't send the values into the method, you send a single value that is an array.

Use arr[i] to get an item in the array instead of slice.

Also, you should declare the variable largest inside the function, so that the function can be used more than once without retaining the result from previous calls. The variable i should also be local.

function largestOfFour(arr) {
  var largest = [];
  for (var i = 0; i < arr.length; i++) {
    largest.push(Math.max.apply(Math, arr[i]));
  } 
  return largest;
}

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

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

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

1 Comment

I noticed that and tried something similar but I still had the tempSub code in there. That is exactly what I am trying to do and thanks for explaining it so clearly.
3

You could use Array.map to run a function on each array that gets its largest value:

function largestOfFour(arr) {
    return arr.map(function(v){
        return Math.max.apply(Math, v);
    });
}

Or using ES6 arrow functions (which not many browsers actually support):

function largestOfFour(arr) {
    return arr.map(v => Math.max.apply(Math, v));
}

4 Comments

That is a very nice alternative way to accomplish what I am trying to do, thanks for sharing. Much more eloquent than my rudimentary code above.
Note that the map method is not supported in all browsers either. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
@Guffa: True, but I think you can safely assume no one's using IE < 9, no? Also there's a polyfill if you really need to support IE 8.
@RocketHazmat: You can definitely not safely assume that. This for example says that about 11% is using it: netmarketshare.com/… Other sources have other statistics for IE 8, but they are never zero.

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.