Return Largest Numbers in Arrays - code neatness

Return Largest Numbers in Arrays - code neatness
0

#1

Tell us what’s happening:
Hello,

actually, I find out the solution of this assignment. Anyway, I’ve noticed that my solutions are usually different than suggested in “Get a hint” section. Should I be worried? My code works, but those seem to be more “neat”. Is this '‘neatness’ part important in front-end job? Or it’s mostly about finding the solution and problems resolving?

Your code so far

function largestOfFour(arr) {
  // You can do this!
  for (i=0; i<arr.length; i++) {
    arr[i] = arr[i].sort(function(a,b) {
      return b-a;
    });
  }
  
  var newArr = [];
  for (i=0; i<arr.length; i++) {
    newArr.push(arr[i][0]);
  }
  
  return newArr;
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36.

Link to the challenge:


#2

Don’t worry if you take different paths to the solution for now. I answered a question like this just the other day, and the answer applies to your question too: Chunky Monkey - Very different solution


#3

One thing which would make your solution “neater” is skipping the extra for loop.

function largestOfFour(arr) {
  // You can do this!
  var newArr = []; // initialize newArr before entering for loop
  for (i=0; i<arr.length; i++) {
    arr[i] = arr[i].sort(function(a,b) {
      return b-a;
    });
    newArr.push(arr[i][0]);  // adding this line avoids the extra for loop
  }
  return newArr;
}