.push fails largestOfFour(arr)

**not sure if this is a peculiarity of browser editor **
if i use the .push method the console.log output of the returned value (big) looks like the same array, but the test fails [except first test parameter (top of list @ bottom left)]
.push method gives below.
anyone know why?

// running tests
largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]) should return [27, 5, 39, 1001].
largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]) should return [9, 35, 97, 1000000].
largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]) should return [25, 48, 21, -3].
// tests completed
// console output
[ 27, 5, 39, 1001 ]
[ 27, 5, 39, 1001 ]
  **Your code so far**

function largestOfFour(arr) {
for (let i=0;i<arr.length;i++){
  let sub=arr[i]
  let temp=sub[0]

    for(let j=1;j<=sub.length;j++){
      if(sub[j]>temp){temp=sub[j]}
    }
  big.push(temp)
//  big[i]=temp
}
return big
}
let big = []
largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
console.log(big)
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

HI @danCamalMan !

You need to move this

at the top of your function before these loops.

function largestOfFour(arr) {
let big=[]

The big variable should not be outside the function like you have it.

Also move this console.log just before the return statement.

  console.log(big)
  return big
}

Then all of your tests will pass.

yes, it does!
thank you.
do you know why?
it’s the declaration of array, big. but i thought if its global and i don’t confuse with another big variable that would be fine (although i see how it would usually make sense to keep it in the function)

My guess, is that the tests were failing because you weren’t going to be using the big arr outside the function.
To me it didn’t make much sense to declare it outside the function if it not going to be used anywhere else.

But maybe someone with more experience has a more official explanation.

1 Like

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