0

I'm a beginning coder who has been learning through FCC. I'm currently trying to solve an algorithm challenge in which I must return an array consisting of the largest number from each subarray. Explanations of what I'm missing or doing incorrectly are greatly appreciate, as it is currently only returning the zero index of the first array.

my code so far:

function largestOfFour(arr) {
   var i = 0;
   for (i = 0; i < arr.length; i++){
      arr[i].sort(function(a, b){return b-a});
      return arr[i][0];     
   }
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
1
  • becuase return exits. You need to use something like map() and using sort() works, but it does a lot more iterations than that are needed. Commented Jun 24, 2017 at 1:31

3 Answers 3

1

You are returning from the very first iteration of the loop. (return terminates the whole function thus the loop is useless). You need to return after the loop finishes, thus you need a new way of storing the results (another array).

Since you are looking for the maximum number, Math.max will be better than sort:

function largestOfFour(arr) {
   var i = 0;
   var results = [];                              // the results array (the array that will contain the maximum numbers)
   for (i = 0; i < arr.length; i++) {
       var max = Math.max.apply(null, arr[i]);    // get the maximum number for the current array (if you want to use sort, it won't be a problem but Math.max is better)
       results.push(max);                         // add this maxumum number to the results array
   }
   return results;                                // when the loop finishes (we got all the maximums), then return
}

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

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

1 Comment

Thank you for the thorough explanation! I'm still learning how to get the output I want and now realize how the return terminates it early.
1

You could use map function on the array, and for each sub array take the maximum value:

arr.map(x => Math.max(...x))
# [ 5, 27, 39, 1001 ]

Or:

arr.map(x => Math.max.apply(null, x))
# [ 5, 27, 39, 1001 ]

Comments

-1

The function is not executing for the entire length of the array as it encounters return statement after first iteration.In this case the objective is to find largest from each of the sub array, so the function to find largest has to be triggered on each and every subarray

 // find largest in each subarray.the largest will be at 0 index
function largestOfFour(arr) {
  return arr.sort(function(a, b) {
    return b - a
  })[0];


}
var array = [
  [4, 5, 1, 3],
  [13, 27, 18, 26],
  [32, 35, 37, 39],
  [1000, 1001, 857, 1]
]

// looping through the array
array.forEach(function(item) {
  //item will be each subarray
  console.log(largestOfFour(item))

})

2 Comments

why down vote? Please clarify it will help it improving answer
Thank you! This really helped me to see why my code was incomplete. I'm not familiar with the forEach() method yet, but I'll do some research for a better understanding.

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.