Ask coding questions

← Back to all posts
How do I swap the values of my function?

function roadRage() {
let rightLane = "Chevy";
let leftLane = "Honda";

``````//Write your code here to make the cars switch lanes
let Chevy = leftLane;
let Honda = rightLane;
return (`That \${rightLane} and \${leftLane} won't pick a lane`)``````

}

roadRage();

Answered by SolarBoom (71) [earned 5 cycles]
View Answer
Commentshotnewtop
SolarBoom (71)

Please mark this as answered if I helped your problem 👍

Swapping the value of two variables normally takes three lines and a temporary variable. What if I would tell you there was an easier way to do this with JavaScript?
There are two types of conditions for such a task. Classical condition: change two numbers. The more difficult task: to change the two any types (string, float, object, other…).
Traditional method
The goal is to swap the values of a and b (all types).
let a = 1,
b = 2,
c = 0;
c = a;
a = b;
b = c;
Of course, we’ve introduced another variable called c to temporarily store the original value of a during the swap. But can we do it without c? Yes, we can!
Just do it! =)
Solution only for integers
Let’s start with the first task: to change the two integers.
With mathematical operations
As question was precious “Only inegers variables, not any objects. “, the answer will be also precious:
a = a + b
b = a - b
a = a - b
or
a += b
b = a — b
a -= b
XOR
Since these are integers, you can also use any number of clever tricks to swap without using a third variable. For instance you can use the bitwise xor operator:
a = a ^ b
b = a ^ b
a = a ^ b
or
a ^= b;
b ^= a;
a ^= b;
This is called the XOR swap algorithm. Its theory of operation is described in this Wikipedia article. Forgot to tell that this works for sure only with integer. Assumed the integer variables from question’s thread.
More details http://chris-taylor.github.io/blog/2013/02/25/xor-trick/
Hacks & tricks
Single line swapping with addition
a = b + (b=a, 0)
This solution uses no temporary variables, no arrays, only one addition, and it’s fast. In fact, it is sometimes faster than a temporary variable on several platforms. It works for all numbers, never overflows, and handles edge-cases such as Infinity and NaN.
It works in two steps:
1. (b=a, 0) sets b to the old value of a and yields 0
2. a = b + 0 sets a to the old value of b
Another single line swapping
b=a+(a=b)-b
Single line swapping with XOR
a = a^b^(b^=(a^b))
Solutions for all types
Second task: to change the two variables with any types.
Classic one-line method
This trick uses an array to perform the swap. Take a second to wrap your head around it:
a = [b, b=a];
There are a few things happening here, so if you’re still having trouble understanding how or why this works, read explanation:
We’re utilizing an array where the first index is the value of a and the second index is the value of b
a is set to the value of b when the array is created
b is set to the first index of the array, which is a
One more tricky one-line solution
Using ES6 self executing arrow functions:
b = (a=>a)(a,a=b);
or ES5+ immediately invoked function
b = (function(a){ return a })(a, a=b);
ES6+ method
Since ES6, you can also swap variables more elegantly. You can use destructuring assignment array matching. It’s simply.
[a, b] = [b, a]

AndresHaro1 (0)

@SolarBoom You answer more of what I was expecting.

With all this info, I was able to solve my issue. Now the best result is to type the following code:

//Write your code here to make the cars switch lanes
[rightLane, leftLane] = [leftLane, rightLane]

SolarBoom (71)

@AndresHaro1 Glad to help you 😄😄😄

mwilki7 (152)

2 variables can't give and receive a new value at the same time so you need a third variable to do some place holding

Here is an example of swapping 'a' and 'b':

``````swap(a,b)
{
temp = a        <--- save a copy of 'a' for later
a = b
b = temp        <--- can't do 'a' because 'a' currently equals 'b'
}``````
Vandesm14 (497)

In the repl: https://repl.it/@Vandesm14/Swap-Variables-Values
I have created a function `swapLanes()` which takes the right and left lanes and returns them as an array. This array is then used to reverse the order of the cars by using the index of the leftLane and storing it to the rightLane and same for the other.

Please mark this as answered if I helped your problem 👍