0

Can someone tell me what is wrong with this code? I am trying to return the largest numbers from four separate arrays.

function largestOfFour(arr) {
  var longList = [];
  for (var i in arr){
    var longest = 0;
    for (var x=0; x<i.length; x++){
      if (i[x] > longest){
        longest=i[x];
      }
      longList.push(longest);
    }
  }
  return longList;
}

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

EDIT: Lye Fish provided the answer below. Here is the new code:

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

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
6
  • what will be the output and what is showing ? Commented Sep 9, 2015 at 1:52
  • 3
    what's the problem you've encountered? Commented Sep 9, 2015 at 1:52
  • Protip: Never use for(var k in ...) for an array. It does not do what you think it does. Btw, usually people would use the variable name k or key to indicate that it gives you the key instead of the value as in some other languages like Python. Commented Sep 9, 2015 at 1:58
  • stackoverflow.com/questions/500504/… Commented Sep 9, 2015 at 1:59
  • your_array.map(Function.apply.bind(Math.max, Math)) Commented Sep 9, 2015 at 2:05

2 Answers 2

3

for(var i in arr) gives indices, so i[x] ends up being 0[0] instead of being arr[0][0].

Use for (var index=0; index<arr.length; index++) { i = array[index]; ... }, or (unless you are supporting the ancients) Array.each, for iterating through array items (or better yet, Array.reduce):

var arrayMax = function(arr) {
  return arr.reduce(function(a, e) { return Math.max(a, e); });
}
arrayMax(arr.map(arrayMax));
// ==> 1001

Or, better yet:

var arrayMax = function(arr) {
  return Math.max.apply(Math, arr);
}
arrayMax(arr.map(arrayMax));
// ==> 1001

EDIT: It might be I read the question too quickly; if you want the maximum of each sublist individually, Lye Fish provided it in the comments.

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

3 Comments

I didn't feel like answering but using Math.max.apply was what I thought when I saw this problem. Stylistically do you prefer to explicitly pass Math as the this reference? Math.max.apply(null, arr); yields the same result.
@t3dodson: Shouldn't matter, but Math.max.apply(Math, [1, 2]) is the actual equivalent of Math.max(1, 2); while there should be no surprises, puts me into good habits, I suppose.
Thank you for your help. Yes, Lye Fish had the perfect answer, but I also appreciate your effort to help.
0
// sort function
var op = function(a, b) { return a < b};

// the input
arr2d = [[8,7,1,78,899],[8,7,1,78,198],[8,7,1,78,598],[8,7,1,78,398]]; 
// output
var result = [];
// process
arr2d.forEach(function(arr) {
   result.push(arr.sort(op)[0]); // sort and return the highest of the numbers
});

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.