Basic Algorithm Scripting - Return Largest Numbers in Arrays

Tell us what’s happening:
I can’t figure out my code passes all but one test. The failed test condition says should return [25, 48, 21, -3] ; however, it would seem [25, 7, 34, 48] should have returned. I am sure I am overlooking something but I can’t figure out what. I can get past this problem but I just am hoping someone can show me where I have an error in my code.
Thanks in advance for any assistance . I attached a picture without comments to make it cleaner to read.

My comments are in the body of this post if you want to see what I was trying to do.

Your code so far

function largestOfFour(arr) {

  let largestNumber = [0,0,0,0]; //Creat largestNumber array to hold return values from subarrays

  for (let i = 0; i < arr.length; i++) { //Initialize first loop by iterating i through arrays

    for (let j = 0; j < arr[i].length; j++) { //Inialize second loop with subArrIndex[j] to iterate through subarrays

      if (arr[i][j] > largestNumber[i]) { //If condition will stop testing when arr[i][j] is no longer greater than largestNumber[i]

        largestNumber[i] = arr[i][j]; //When "if" condition is met, largestNumber[i] will be equal to arr[i][j].
  return largestNumber;

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/111.0

Challenge: Basic Algorithm Scripting - Return Largest Numbers in Arrays

Link to the challenge:

You are checking against 0 but the array has negative values that are less than 0.

You either need an infinitely large negative number to check against, or you need to use the actual numbers you know you are checking against.

I am not sure how to create the loop with an infinite large negative number. I will try the reduce method. I thought for sure the code I had would have worked.
Thanks for the reply.

The link I gave has something you can use. Or as I/we also suggest, just check against the actual numbers being used instead of 0.

Thanks to both of you. I got it to go through. Sometimes I look at these too long and just need to walk away a moment. I appreciate the assistance.

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