Largest Numbers in Arrays issue

I pass the challenge but I have a question.
My code is only for 4 sub arrays. but If I want to find Largest numbers for more than 4 arrays. what I should change or edit on the code below?

My code so far

function largestOfFour(arr) {
let max1 = Math.max(...arr[0]);
let max2 = Math.max(...arr[1]);
let max3 = Math.max(...arr[2]);
let max4 = Math.max(...arr[3]);
let newArr = [];
return newArr;

console.log(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.15; rv:83.0) Gecko/20100101 Firefox/83.0.

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

Hello Abdul. The problem with your current implementation is that is hard-coded; when we say that something is hard-coded we mean something static that will never change it’s behaviour; in your case it will work with only 4 elements; and to make it work you had to do write a lot of variables and actions, all manually. Why don’t you automate that? Let the computer bother, use a for loop:

function largestElements(arr) {

  let biggestElements = [] //store here biggest numbers

  for(let i = 0; i < arr.length; i++){

    //iterate through input array; remember that each arr[i] is actually an array

    biggestElements.push(Math.max(...arr[i])) //use spread operator to find max number, and push it to solution


  return biggestElements


largestElements([[1, 2, 3], [4, 6, 8,1], [8, 22, 33, 6], [25, 335, 47], [124, 635, 777]]) //[ 3, 8, 33, 335, 777 ]
1 Like

Hi @abdulrahman.mhd.anas. You can use a loop or

const newArr = [];
arr.forEach( innerArr => newArr.push(Math.max(...innerArr)))

I thought like that in the first.
but I write same that code but without map method because I don’t know this method. so I saw an error in the output and I decided to find another way to pass the challenge.

Hi @nibble and @doriodavid
As you said, we need to use and Array.prototype.forEach to solve the challenge. How I use these methods and I have not learned these yet in the freeCodeCamp Curriculum ?

Map is a very, very useful method, learning it will help you a LOT. It iterates through an array and performs a function on each of its element. Let me show you it works with some pseudo code:

let arr = [1, 2, 3, 4] //here is the array we want to traverse

//notice, the below function is just a silly function, no different than any other function

function doubleUp(number){

 //it accepts one argument, a number, and returns it multiplied by two

  return number * 2


doubleUp(5) // 10

doubleUp(3) // 6

//now we use map, which look like this

//[Array to be iterated].map([function you want to perform on each element])

//*** But pay attention, map mutates the array *in place*

// is the equivalent of:

for(let i = 0; i < arr.length; i++) {

//iterate through our array

arr[i] = doubleUp(arr[i]) //what you do here is saying: computer, the new value kept at current index becomes the value itself * 2


let array = [2, 3, 4]

//I'll write another version, a bit clearer: 

let newArray = []

for(let i = 0; i < array.length; i++) {



array = newArray //! notice here, our array become the new array

The first time I realized it it puzzled me, I kind of got upset, but then I realized that is far much more better this way because it forces you to go and dig outside of FCC; and that’s something you should do as soon as possible, the FCC curriculum will help you with the first steps, but then you’ll need to study on other platforms…

read the challenge, you have sub-arrays and you need to return an array containing the largest number from each subarray, with that way you will end up with just one number

1 Like

What do you mean @nibble about

I started the JavaScript curriculum from beginning until this Basic Algorithm Scripting section and I didn’t face map method or forEach method?

@abdulrahman.mhd.anas. Check this

yeah he is right, probably because your’e too experienced and a lot of time went since you opened the js curriculm… the map is covered in the functional programming section which is after the basic algorithms


Do you mean the freeCodeCamp is not a complete platform to learn code?

it’s a good platform to learn code, but obviousley not complete, there are just so many languages, meethods and commands that you can’t learn it all from here

Sure I agree @harel_avv. I mixed up the sections a bit. @abdulrahman.mhd.anas You can continue with an ordinary for loop. Array methods are ahead of Basic JS algorithms. Just disregard the solution I am suggesting using forEach above if it confuses you.

Ah! sorry i mistook!

I mean is freeCodeCamp not a complete platform to learn javaScript?

it doesn’t teach everything about the language, but you can find everything about the language in the documentation. It teaches the basics tho.

Look, I’ve been studying JS for the last six months… I can tell you that I haven’t come back to the curriculum in… probably three months or more (I’ve finished it by the way, I just keep procrastinating the last project… in the meanwhile I do practice on codewars)… Why so? Because there’s just too much to be covered… the FCC covers, maybe… 20% -on a basic level- of the whole language? But listen: it’s absolutely better to start learning from basics, even a bit… ‘superficially’… For example, before you start studying what really is a lexical environment, how data is stored into a variable, what is hoisting and so on, you better understand how to simply use a variable; which you did, in your solution. By the way you could have used destructuring:

let [a, b, c, d] = [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]
//work like this:         ^---------------^------------------^---------------------^
//                                         |                             |                                 |                                        |
//                                         a                            b                                c                                       d

So, before you really get under the hood, into theory, take some time practicing algorithms, learn how to use your tools and, then and only then, try figuring out why do they work.

The JS section of the curriculum is starting to show its age a bit and would likely benefit from an overhaul (but that takes a lot of work). Challenges have been updated and some reshuffling has been made but the current structure and the order of everything are likely not how you would lay it out if you were to write it from scratch starting today. It’s also very challenging to make sure everything comes in a logical order and that you keep building upon concepts learned previously.

You should certainly learn about normal for loops before learning about array methods, but I don’t think I would wait to teach about them until the functional programming section. A dedicated section on just array methods might be useful. ES6 is no longer new and modern JS makes heavy use of array methods, and ES6 features, like arrow functions (especially for callbacks) and rest/spread/destructuring. It’s just how and when you teach about these features that can be a bit challenging.

Overall, I think the curriculum does a good job of teaching JS and the progression order is still pretty good. It’s just become hard to augment it, sort of like how a codebase can become hard to update and refactor without at some point needing to just start fresh.

A while ago I decided to practice JavaScript on a coding challenges site like: codewars along with freeCodeCamp .
I decided to choose edabit but I also see great sites like codewars . So I am confused of choosing edabit or codewars .
Is codewars appropriate for beginners?
What do you recommend codewars or edabit ?