Bonfire Challenge: Return Largest Numbers in Arrays

Bonfire Challenge: Return Largest Numbers in Arrays
0

#1

Hello! I have two different answers for this challenge, one results in ‘[ 5, 27, 39, 1001 ]’, the other in ‘5, 27, 39, 1001’. I can’t figure out how to remove the apostrophe while keeping the brackets. Here are my two separate pieces of code:

This is the one with ‘[]’:


function largestOfFour(arr) {
  arr.forEach(function(arr1) {
    return arr1.sort(function(a, b) {
      return b - a;
    });
      });
      
      arr.forEach(function(arr1) {
       return arr1.splice(1);
      });
    
   var result = arr.join();
   
   return '[' + result + ']';
   
}

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

This is the one with ‘’:

function largestOfFour(arr) {
  arr.forEach(function(arr1) {
    return arr1.sort(function(a, b) {
      return b - a;
    });
      });
      arr.forEach(function(arr1) {
        return arr1.splice(1);
      });
  return arr.join(" ");
}

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

Thank you for your help! -Marie


#2

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

In both of your solutions you are creating a string instead of an array of values to use in the final return statement.

The problem is in the following:

    arr.forEach(function(arr1) {
        return arr1.splice(1);
      });

Your use of splice here does not change the fact that arr is still an array of arrays, the only difference is that each nested array now only has 1 element in it. You then try to turn it into a string and pass it back and the very end, but this will fail the tests, because you should be returning an array of values and not a string that “looks” like an array.

In the first section of each solution, you are sorting each array, so that the first element in each nested array is the largest number, instead of splicing each nested array, why not reference the first element in each array and add it a new array. To make this work, you would need to declare an empty array before the second forEach. You could call it result if you want, then use the push function to add only the first element of each nested array.

If you are asking yourself, “how do I get the first element of an array?”, then you need to think back when you first learned about arrays. Arrays are zero indexed, so from left to right, each element is ordered with an index starting at 0 (zero) for the first element. Take this fact and push the first element of each nested array to your result array and then return result at the very end.


#3

Thank you, Randell, for the suggestions; I will give it a try.