Mode entretien. Minuteur de 10 min, sans indices ni autocomplétion
Three smallest numbers
MoyenArrays10 min
Write a function threeSmallest(numbers) that returns the three smallest numbers of an array.
numbersis an array of integers. They can be positive, negative or0.- Return a new array with the three smallest numbers, from small to large.
- Equal numbers count separately:
[3, 3, 1]gives[1, 3, 3]. - If the array has fewer than 3 numbers, return all of them, from small to large. An empty array gives
[]. - Compare the numbers by value:
10is larger than9. - Do not change the input array.
If you sort, keep in mind that sort changes (mutates) the array it is called on and needs a compare function to sort numbers by value.
Exemples
threeSmallest([5, 1, 4, 2, 3])Renvoie [ 1, 2, 3 ]
threeSmallest([3, 3, 1])Renvoie [ 1, 3, 3 ]
threeSmallest([8, 2])Renvoie [ 2, 8 ]
Chargement de l'éditeur...
- Entrée
threeSmallest([ 5, 1, 4, 2, 3 ])Attendu[ 1, 2, 3 ] - Entrée
threeSmallest([ 3, 3, 1 ])Attendu[ 1, 3, 3 ] - Entrée
threeSmallest([ 8, 2 ])Attendu[ 2, 8 ]
Les tests cachés se lancent à la soumission