Return Largest Numbers in Arrays issue

Tell us what’s happening:

My code for this challenge returns the correct values, but the commas appear one digit off from what the “expected” solution is supposed to be, which is frustrating as it means my code is not passing the challenge. I tested the code in repl.it and it appears correctly. Below is my code - am I actually in error here, or is this just an issue with how the challenge is testing my code? Thank you.

Link to the challenge:
https://www.freecodecamp.org/challenges/return-largest-numbers-in-arrays

My code:

var newArray= [0, 0, 0, 0];

function largestOfFour(arr) {

  
  for (i = 0; i < 4; i++) {
    console.log ("Analyzing array number " + i);
    for (j = 0; j <4; j++) {
   
   console.log ("Analyzing subarray number " + j);
    if (arr[i][j] > newArray[i])
    {
      console.log("Old highest value in array number " + [i] + " is " +  newArray[i]);
      console.log("New highest value in array number " + [i] + " is " + arr[i][j]);
      
      newArray[i] = arr[i][j];
    }
    }
  }

  return newArray;
}

largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]);

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums


Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the new value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2
1 Like

You’re calling split with ("") this separator on the string.

newArray = newArray.toString();

returns “5,27,39,1001” as a string and when you call split("") it will split all the characters of the string so it returns like [“5”,",",“2”,“7”,","…] means ", " is also one of those characters in the string.

And I don’t think you need to call toString and split() on newArray :wink:

1 Like

Sorry, that split part hadn’t been part of my code, please ignore it. I had done that for testing purposes. I removed it from my example.

Exactly what @ArielLeslie said. You are mutating the variable newArray each time the test runs

i.e. largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]) returns [5,27,39,1001]

BUT the next test REMEMBERS these new values you just changed in variable newArray so when largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]) runs it returns [27,27,39,1001]

Do you see? Now how can you fix that? Check @ArielLeslie example :wink:

1 Like

Thanks to all for the assistance - I simply moved the newArray variable declaration to inside the function and it passed!

1 Like