Variable Swapping

Easy+20 XP

Mission Briefing

Two ancient relics have been placed on the wrong pedestals! You must safely swap their positions without dropping either of them.

The Concept: Swapping Variables

Imagine holding a sword in your right hand and a shield in your left. To swap them, you can't just move them at the same time—you need to place one on a table (a temporary variable) first. In code, a third variable temporarily holds a value so it isn't overwritten.

The Objective

Your function receives parameters a and b.

  1. Create a temporary variable to hold the value of a.
  2. Swap the values of a and b.
  3. Return them as an array [a, b] in their new positions.

01EXAMPLE 1

Inputa = 5, b = 10
Output[10, 5]

02EXAMPLE 2

Inputa = "cat", b = "dog"
Output["dog", "cat"]

Constraints

  • Return the swapped values as an array [a, b].
FundamentalsLogicVariables
JavaScript
Loading...
3 Hidden

Input Arguments

a5
b10

Expected Output

[10,5]

Click RUN to test your solution