Value Exchange
EasyAcc. 92.1%
+20 XP 8
Swapping Places
Sometimes we need to exchange the values of two containers, like swapping duty shifts of two workers. To do this without losing data, we use a third empty variable as temporary holding.
The Assignment
Your function gets two variables, a and b.
- Declare a temporary variable named
temp. - Use it to safely swap the values so that
atakesb's value, andbtakesa's old value. - Return them as an array:
[a, b].
01EXAMPLE 1
Input
a = 5, b = 10Output
[10, 5]Explanation: After the swap, a is 10 and b is 5.
Constraints
- Use a temporary variable if possible.
- Return the result as [a, b].
FundamentalsVariablesLogic
JavaScriptSystem handles I/O — write your function only
Loading...
5 Hidden
Input Arguments
a5
b10
Expected Output
[10,5]
Click RUN to test your solution