Return Largest Numbers in Arrays_ Something wrong

Return Largest Numbers in Arrays_ Something wrong
0.0 0

#1

Tell us what’s happening:

I could not return [25, 48, 21, -3]. What is my wrong ?

Your code so far


function largestOfFour(arr) {
  var largestNumber = [0, 0, 0, 0];
  for (var arrayIndex = 0; arrayIndex < arr.length; arrayIndex++) {
    for (var subArrayIndex = 0; subArrayIndex < arr[arrayIndex].length; subArrayIndex++) {
      if (arr[arrayIndex][subArrayIndex] > largestNumber[arrayIndex]) {
        largestNumber[arrayIndex] = arr[arrayIndex][subArrayIndex];
      }
    }
  }
  return largestNumber;
}
largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]);

**Your browser information:**

User Agent is: <code>Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36</code>.

**Link to the challenge:**
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/return-largest-numbers-in-arrays

Something is wrong in Return Largest Numbers in Arrays
#2

You compare negative numbers against 0, and 0 is bigger than any negative number (the last subarray has all negatives, and largestNumber[3] is initialized with 0).


#3

Thanks for the answer, I have the same issue too, but my solution pretty much aligns with what’s on the freeCodeCamp blog post: https://medium.freecodecamp.org/three-ways-to-return-largest-numbers-in-arrays-in-javascript-5d977baa80a1

So I guess the solution on the blog is also wrong? Is there a way to fix this with a similar solution?

Your code so far


function largestOfFour(arr) {
  var biggestNumber = [0, 0, 0, 0];
  for (var i = 0; i < arr.length; i++) {
    for (var j = 0; j < arr[i].length; j++) {
      if (arr[i][j] > biggestNumber[i]) {
        biggestNumber[i] = arr[i][j];   
      }
    } 
  }  
  return biggestNumber;
}

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

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/return-largest-numbers-in-arrays


#4

Nevermind I figured it out, someone else pointed out the same issue on the blog post:

So you can fix the negative value compare by “initializing your comparisons to the first element in the array”, like so:
var biggestNumber = [arr[0][0], arr[1][0], arr[2][0], arr[3][0]];


#5

You can fix the negative value compare by “initializing your comparisons to the first element in the array”, like so:
var largestNumber = [arr[0][0], arr[1][0], arr[2][0], arr[3][0]];


#6

This solution you posted passes the test, but requires that you know the size of the sub-arrays beforehand so that you can initialize every index. A more programmatic solution would be

function largestOfFour(arr) {
  var biggestNumber = []; // no need to pre-initialize

  for (var i = 0; i < arr.length; i++) {
    for (var j = 0; j < arr[i].length; j++) {
      // if index === 0, it's automatically the highest
       if (j === 0) biggestNumber[i] = arr[i][j] 
      // otherwise return the greater value, negatives included
       else biggestNumber[i] = Math.max(biggestNumber[i], arr[i][j])
    }
  }

  return biggestNumber;
}


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

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max


#8

The solution to this is quite simple. if you sort each array from lowest to highest(store the sorted array in a new array “newArr” ), the last number will always be the greatest, or sort from highest to lowest and the first number will always be the greatest. Here is my code.

ASCENDING ORDER

function largestOfFour(arr) {
let newArr = [];
let highest = [];

//sort array
for(let i = 0; i < arr.length; i++){
arr[i].sort(function(a, b){
return a - b;
});
newArr.push(arr[i]);
}//end of for loop for sorting array

//pick the last number from each array
for( let j = 0; j < newArr.length; j++){
highest.push(newArr[j][newArr[j].length-1]);
}

return highest;
}

OR

DESCENDING ORDER

function largestOfFour(arr) {
  let newArr = [];
  let highest = [];
//sort array
  for(let i = 0; i < arr.length; i++){
    arr[i].sort(function(a, b){
      return b - a;
    });
    newArr.push(arr[i]);
  }
//end of for loop for sorting array

//pick the first number from each array
  for( let j = 0; j < newArr.length; j++){
      highest.push(newArr[j][0]);
  }
  
  return highest;
}


#9

@KvNGCzA Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.


#10

Ok. Sorry about that. Thanks