Basic Algorithm Scripting - Return Largest Numbers in Arrays

Tell us what’s happening:
Describe your issue in detail here.

Your code so far

function largestOfFour(arr) {
const results = [];
  for (let i = 0; i < arr.length; i++) {
    let largestNumber = arr[i][0];
  }
  for (let j = 1; j < arr[i].length; j++) {
    if (largestNumber > arr[i][j]) {
      largestNumber = arr[i][j];
    }
  }
  results[i] = largestNumber; 
  
  
  
  
  
  
  
  
  
  
  return results;
}

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 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/112.0

Challenge: Basic Algorithm Scripting - Return Largest Numbers in Arrays

Link to the challenge:

Which issue do you want help with? There are more than one.

For this you need a nested loop
You also need To use Number.NEGATIVE_INFINITY as an intial valuse to compare with incase of negative numbers.

Mod Edit: SOLUTION REMOVED

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Use of negative infinity isn’t required. You can use the first element of each subarray as an initial guess.

It’s my first time here I didn’t know that it’s not allowed to post the solution

Why I is undefined and why largestNumber also is undefined while I assign i at the for loop and largestNumber assign to arr[I][j]

results[i] = largestNumber
i is undefined in this statement because it is not inside the for loop where ‘I’ is defined.
the variable ‘largestNumber’ is also used out of scope.
Check to make sure your curly braces properly nest in your code

Thanks it was at the curly braces and the condition

You’re welcome! Happy to help!

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