How do I swap the values of my function?

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`)``````

}

You are viewing a single comment. View All
[deleted]

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âŚ).
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]

[deleted]