Return largest num algorithm

Return largest num algorithm
0

#1

I feel like i;m not good enough . I basically inspired myself from other sources for almost all alogrithms problems till now. Maybe i just spend too less time on it. Like,if i can’t make it in 5 minutes i start googling it or checking the FCC solution code. Here is the return largest number alogrithm,i couldn/t do it so i basically had to write their solution. Can someone explain me the code,I really don’t understand it.

function largestOfFour(arr) {
  // You can do this!
  var arrays= [];
  for (i = 0 ; i < arr.length;i++){
    var biggestNum = arr[i][0];
    for (var x = 1 ; x < arr[i].length; x++) {
      if (arr[i][x] >biggestNum) { 
        biggestNum = arr[i][x];
      }
    }
    
    arrays[i] = biggestNum;
  }
  return arrays;
}

#2

You have to explain what is the request, what is input and what do you want like output.


#3

@GummyGod - I have hidden your solution to not spoil it for others still working on the challenge. Below, I have put the Code Explanation which you can also find on the same page where the above solution is posted in the FCC Guide.

Can you be more specific about what part of their explanation of the code you do not understand?

Code Explanation:

  • Create a variable to store the results as an array.
  • Create an outer loop to iterate through the outer array.
  • Create a second variable to hold the largest number and initialise it with the first number. This must be outside an inner loop so it won’t be reassigned until we find a larger number.
  • Create said inner loop to work with the sub-arrays.
  • Check if the element of the sub array is larger than the currently stored largest number. If so, then update the number in the variable.
  • After the inner loop, save the largest number in the corresponding position inside of the results array.
  • And finally return said array.

#4

Spend more time if you really want to learn. I got caught up in that trap as well and eventually it will catch up to you. Soon you will find a problem (real-world) and there won’t be a guide to help you.

I encourage you to go through the suck and then you will look back and laugh at how simple things tripped you up. Do yourself the favor, your future-self will thank you.

For what it’s worth, this is what I came up with, might be easier to digest…

function largestOfFour(arr) {
  return arr.sort((a, b) => a - b)[arr.length - 1]
}

#5

Hahahahahah thanks sir u really helped me(No sarcasm)
lIl pump and tyler 1haha