Question of Return Largest Numbers in Arrays

Tell us what’s happening:
Dear all,
I’m wondering why the following code won’t work. Instead, the code next will work.

  1. Why can’t I put
    let largestNum = arr[i][0];
    after
    for (let j=0; j<arr[i].length; j++){

  2. Where should I put return ans;. It seems like it matters that return ans; should only be added before certain curly bracket, but I’m often confused after which one.
    Thanks for helping

    Your code so far


function largestOfFour(arr) {
let ans =[];
for(let i= 0; i< arr.length; i++){
  for (let j=0; j<arr[i].length; j++){
  let largestNum = arr[i][0];
    if (arr[i][j]> largestNum){
      largestNum= arr[i][j];
    } 
  } ans[i] = largestNum
}
return ans;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
function largestOfFour(arr) {
  let ans =[];
  for(let i= 0; i< arr.length; i++){
    let largestNum = arr[i][0];
    for (let j=0; j<arr[i].length; j++){
      if (arr[i][j]> largestNum){
        largestNum= arr[i][j];
      } 
    } ans[i] = largestNum
  }
  return ans;
}

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_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.0 Safari/605.1.15

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

You want to check every array in the array argument provided to the function and find the largest number each one contains, then push it within the array you will return as a result. The method we use to find the largest number in every array is to iterate thru every element in a given array and establish which is the largest. Only once we have established that, we can put that element within the array we will return as result. Once all arrays have been checked and their largest elements pushed inside the result array, we return that result from the function. This is the execution order of our logic, you need to apply it in your code, see where a largest number is finially established of an array so it can be added in the result; see when our result array is complete and all largest numbers included

We wanna declare a largestNum variable before we start testing the respective array numbers. We use its first element as an initial value, then we compare it with every other element. If another element is larger, its assigned as the value of largestNum and any consecutive elements are being compared towards it. Keep in mind all that can be achieved in different variations and slightly more efficient. If you were to declare let largestNum = arr[i][0] on every iteration(for every element we test in the array), we will always compare the current element with the first element in the array and not with the largest element so far, so in the end, what we will get as a result(if we take in mind the remaining logic of your code), will be the largest number between the first element and the last element in the array, as we initiate a new value for the largest element on every loop.

Like i mentioned, we should return the resulting array containing all largest numbers, once all of the are being established; once all arrays have been looped thru and each has its largest number included in the result. Its important to be able to follow the curly brackets segments and know where a loop end, when we are within a condition body and when a function body is concluded. Dont worry, its hard for the untrained eye and this comes with practice and time and even experienced eyes can have trouble follow up a code flow.

Thank you so much for your answer! It is really helpful

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.