Returning Largest Numbers in Arrays CHALLENGE

Returning Largest Numbers in Arrays CHALLENGE
0.0 0

#1

Hey guys, I’m struggling with this algorithm challenge. Am I close or way off base here?

Please and thank you!


#2

As soon as a return statement is reached, no further code is executed. In other words, return largeNum on line 11 means that your function returns that single number and it’s all over.


#3

OK, first of all your for loops are set up correctly, which is a good first step. After that, there are some issues. Let me help map out the algorithm for you, so you can see where your solution differs.

Basically, you want to loop through arr (which you are doing with your for loop with variable i) and then loop through each element of arr (which are also arrays which you are doing with your for loop with variable j) and push the largest of each of these subarrays into newArr.

Currently, in your nested loop (j variable) you are comparing each item with largeNum and then assigning the item to largeNum if the item is larger than largeNum. The problem here is you assigned a value of 0 in the very beginning of the function. Using your train of thought, you really should move this assignment to right before you start the nested loop. That way, you reset largeNum back to zero each time, before you start making your comparisons. Note: Even though resetting largeNum to zero will pass the FCC tests, this logic would fail if the subarrays had negative numbers in them. I will let you think about this fact and come up with an alternative assignment to largeNum before entering the nested loop.

The next problem in your code is when and how your are trying to push the largest numbers of each subarray to newArr. You want to push to newArr right after the nested loop, because that is when largeNum will have the correct number to push. Also, you do not want to return largeNum as you are doing on line #11, but instead use newArr.push(largeNum).

The last issue is after all the loops are complete, you simply want to return newArr.

Hopefully this well help you complete this challenge. Please reply back with any more questions.


#4

Ahh Okay. Thank you @rmdawson71! What you said makes sense. I moved line 11 to line 10 and changed it to push largeNum to newArr instead of returning, and moved largeNum to right before my nested loop. I’m confused as to what I should assign to it though since, as you said, the logic fails with negative values–I just left empty quotes? But this code is still not passing the challenge. When I look at the console output, it’s pushing the TWO highest values of each subarray to the newArr instead of one?


#5

Do me a favor and copy/paste your code into your next post. Once you have pasted the code, put three backticks on the lines immediately before and after the code and it will make it more readable on the forum. That way, I can copy the code in my editor to test things out quickly.


#6

Actually, before I answer what to do with assigning a value for largeNum, just keep it zero for now and let’s focus on what is wrong with your code right now. Ask yourself “Where should I push the largeNum to newArr?” With your current logic, you are pushing a number each the arr[i][j] is greater than largeNum within the nested loop. You only want the final value of largeNum. You only have to move one line to another and you have it.


#7
function largestOfFour(arr) {
  // You can do this!
  var newArr = [];
  for (var i = 0; i < arr.length; i++) {
    var largeNum = '';
    for (var j = 0; j < arr[i].length; j++) {
      if (arr[i][j] > largeNum) {
        largeNum = arr[i][j];
        newArr.push(largeNum);
      }
        
    } 
  }
  return newArr;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
    `indent preformatted text by 4 spaces`

#8

See my previous post and you should be able to figure it out. Let me know when you have passed all the tests and then I will share the best practice for how to modify your code to except negative values for the sub-arrays.


#9

Okay, I fixed it! Please school me on those best practices haha.

function largestOfFour(arr) {
  // You can do this!
  var newArr = [];
  
  for (var i = 0; i < arr.length; i++) {
    var largeNum = 0;
    for (var j = 0; j < arr[i].length; j++) {
      if (arr[i][j] > largeNum) {
        largeNum = arr[i][j];
        
      }
        
    } newArr.push(largeNum);
  }
  return newArr;
}

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

#10

What about:

var largeNum = arr[i][0];  // assigns the first element of each sub-array to start the comparison

Also, since you have already learned about the map and reduce functions, see if you can understand how the following code accomplishes the same thing as your code:

function largestOfFour(arr) {
  return arr.map(function(subArr) { 
    return subArr.reduce(function(largeNum,num) {
      return Math.max(largeNum,num);
    });
  });
}

Also, you can replace the following in the reduce:

return Math.max(largeNum,num);

with:

return num > largeNum ? num : largeNum;