Mode entretien. Minuteur de 5 min, sans indices ni autocomplétion
Swap two numbers without a third variable
FacileBasics5 min
Write a function swapPair(pair) that swaps two numbers without a third variable.
A function cannot change the variables of the code that called it, so the two numbers come in an array: pair is [a, b].
- Change
pairitself so that it becomes[b, a], then return that same array, not a new one. aandbare integers from-1000000000000to1000000000000. They can be negative, zero or equal.- No third variable: inside the function do not write
let,constorvar, do not add a second parameter, and do not write another function in it (its parameters would be variables too). - Keep values only in the two items of
pair: do not add items or properties to the array or to the function, and do not create a global variable (temp = pair[0]withoutletcreates one).
Exemples
swapPair([1, 2])Renvoie [ 2, 1 ]
swapPair([-5, 0])Renvoie [ 0, -5 ]
Vérifiez-vous
- No helper variable outside the function either: only the two items of the array are used.
Posé chez ServiceNow · Source
Chargement de l'éditeur...
- Entrée
swapPair([ 1, 2 ])Attendu[ 2, 1 ] - Returns the same array, now swapped
- No let, const or var inside the function
- swapPair has only one parameter
- No global variable is created
Les tests cachés se lancent à la soumission