Mode entretien. Minuteur de 5 min, sans indices ni autocomplétion
Swap two array elements
FacileBasics5 min
Write a function swapItems(items, i, j) that swaps two elements of the array items: the element at index i and the element at index j.
- Change
itemsitself and return that same array, not a new one. The other elements stay where they are. - Indexes start at
0.iandjare always valid indexes ofitems. They can be equal (then nothing changes), andican be greater thanj. - Elements can be numbers or strings (the number
7and the string'7'are different values). The same value can appear more than once: swap by position, not by value. - You may use a helper variable.
Exemples
swapItems(['a', 'b', 'c'], 0, 2)Renvoie [ 'c', 'b', 'a' ]
swapItems([1, 2, 3, 4], 1, 2)Renvoie [ 1, 3, 2, 4 ]
swapItems([5, 6, 7], 1, 1)Renvoie [ 5, 6, 7 ]
Chargement de l'éditeur...
- Entrée
swapItems([ 'a', 'b', 'c' ], 0, 2)Attendu[ 'c', 'b', 'a' ] - Entrée
swapItems([ 1, 2, 3, 4 ], 1, 2)Attendu[ 1, 3, 2, 4 ] - Entrée
swapItems([ 5, 6, 7 ], 1, 1)Attendu[ 5, 6, 7 ] - Returns the same array, now changed
Les tests cachés se lancent à la soumission