# On JS certification, basic algoritms my solution does't work, why?

Basicamente eu faço o código no replit e se funcionar eu ponho no console do FCC, mas no replit ele está funcionando perfeitamente, porém no FCC não. Porque?
Testei com as arrays fornecidas no desafio e todas apresentaram o resultado correto, porém meu código não é aceito

``````  **Meu código:**
``````
``````
function largestOfFour(arr) {
sortedArr = []
for(let i = 0; i <= arr[0].length; i++){
let arr1 = arr[i].sort((a, b) => a - b)
sortedArr.push(arr1.pop())
}
return sortedArr;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
``````
``````  **Informações de seu navegador:**
``````

Agente de usuário: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36 OPR/79.0.4143.73`

Desafio: Retornar os maiores números em arrays

Hi, welcome to the forum!

`sortedArr` is not defined.

just played a bit and don’t think you want the `[0]` in `arr.length`

``````function largestOfFour(arr) {
let sortedArr = []
for(let i = 0; i < arr.length; i++){
arr[i]=arr[i].sort((a, b) => a - b)
sortedArr.push(arr[i].pop())
}
return sortedArr;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
``````

but i wondered: if the `.sort` sorts the elements in place anyway, would we rather use a new variable here? does it matter if that’s how `.sort` works anyways? anyone?

I would say that usually you would not want to modify the arrays/objects passed into a function unless it was specifically designed to do that for some reason. So yes, your question is a good one and if you are going to sort the subarrays you should probably clone them first. The even better choice would be to use a method that didn’t require you to use a temp array in the first place.

1 Like

so you would build the return array from the original without mutating the original?

Yes, exactly. JS provides several methods that allow you to do this in a variety of ways. The one that jumps out to me right away allows you to find the max value in a list of numbers.

1 Like

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