64
u/Freekiehsoes Mar 12 '21
But why
14
u/golovatuy Mar 12 '21
very popular question in the interview )
12
u/Macaframa Mar 12 '21
What is the answer? I don’t see any real reason other than implementing a bubble sort algorithm which is another thing you shouldn’t ever do.
13
u/dudeitsmason Mar 12 '21
When I was younger I might've referenced OPs answer in an interview, given an answer that is technically possible simply because they asked. Now I give, "I wouldn't" to these weird "How would you do x" type questions if the answer is technically possible but not a good idea
2
5
u/adenzerda Mar 12 '21 edited Mar 12 '21
Lots of sorts depend on swaps, not just bubble sort. Insertion sort, heapsort, selection sort, shellsort, quicksort ... not to mention array shuffling, array rotation, string manipulation, and more
24
u/xTRQ Mar 12 '21
I think he means why would you swap a variable.
34
u/Poopingcode Mar 12 '21
In sorting algorithms you need to swap a lot. Tons of uses for swap
2
u/mypetocean Mar 13 '21
It's also used frequently as the cause of the "Freaky Friday flip" trope in film and television media.
8
Mar 12 '21
I actually have an use case for this in mind, I'm building a service based on several map APIs (Mapbox, Google Maps).
When you need to give a coordinate to google maps you must pass it in the format of
setMarker(latitude, longitude)but Mapbox uses a different standard called GeoJSON and requires longitude to come first, so I could technically get my arguments in one place, for both maps, and then just do a switcheroo for Mapbox with[latitude, longitude] = [longitude, latitude]but most likely there's a better way of doing this.11
u/cawcvs Mar 12 '21
The left-hand side of the assignment here is not an array, this won't change the order and you'll just end up with
latitudecontaining the value oflongitudeand vice versa.If you already have
latitudeandlongitudeas variables, just order them however you need when passing them as arguments.12
u/Earhacker Mar 12 '21
To wit:
setGoogleMapsMarker(latitude, longitude); setMapboxMarker(longitude, latitude);2
Mar 12 '21
end up with latitude containing the value of longitude and vice versa.
That's what my goal is, isn't the same thing happening in the screenshot? It ends with
xcontaining initial value ofyandycontaining initial value ofx12
u/cawcvs Mar 12 '21 edited Mar 12 '21
Yes, but do you really want the variable named
latitudeto contain the value of the point's longitude? That would be terribly confusing for anyone reading the code, wouldn't it?-1
2
u/dk1024 Mar 12 '21
This is used in the "array reversal" method for the Leetcode Rotate Array problem I just solved last night.
2
u/amoliski Mar 12 '21
If you have two dates and need them in order:
if(date_a.isBefore(date_b)){ [date_a, date_b] = [date_b, date_a]; }1
u/grantrules Mar 13 '21
Why not just sort.
[date_a, date_b].sort((a,b)=> a.isBefore(b) ? 0 : 1)1
u/amoliski Mar 13 '21
I think you'd want -1 and 1 instead of 0 and 1 there, but yeah, that works too.
1
u/grantrules Mar 13 '21
If there are just two things, 0 is fine since it's already in order. 0 means nothing changes, 1 means swap places.
1
u/rusty_matador_van Mar 13 '21
In many iterations, x becomes y and y becomes new computed value, and goes on.. ex: in games, newPos is equal to currentPos + variable
1
u/Freekiehsoes Mar 13 '21
Well those are vectors
1
u/rusty_matador_van Mar 13 '21
Vector or scalar, stored in variables, and sometimes swapping is needed.
1
u/bablabablaboo Mar 13 '21
Let's says you want to swap the active player on in the game.
manage multiple processes within a single main memory.
main memory utilization.
and many many more
0
12
u/Parent-component Mar 12 '21
I have been asked to swap variables with no intermediate variable in basic JS before.
a=a+b; b=a-b; a=a-b;
This will do it 😃
2
5
4
u/grantrules Mar 13 '21
Basically a simplified version of "You have a 3 gallon bucket and a 5 gallon bucket. How can you measure out 4 gallons"
1
15
u/IxD Mar 12 '21
Compiles to
var _ref = [y, x];
x = _ref[0];
y = _ref[1];
_ref;because breaks on some browsers.
12
Mar 12 '21
what's the last
_ref;for?7
u/IxD Mar 12 '21
I used online compiler for babel - since the code has no side effects it would be optimized to empty code withtout that.
Long version is that it's a statement, not an expression: https://2ality.com/2012/09/expressions-vs-statements.html
2
8
u/FriesWithThat Mar 12 '21
Making me use a semi-colon, somewhere....
let x = 'xerox'
let y = 'yellow'
;[x, y] = [y, x]3
3
3
2
2
u/TheUruz Mar 12 '21
what if instead of const i declare temp as var/let?
1
u/senocular Mar 12 '21
Not much changes.
varandletwouldn't complain if you tried to reassigntemplater on, if that's a concern. And if this code was run in the global scope,varwould addtempas a property of the global object (windowin browsers) if that's a concern.
2
1
u/jayerp May 02 '23
That isn’t SUPER clear to a non-experienced JS dev but I like how it’s that simple.
1
u/turningsteel Mar 12 '21
Honestly, from a readability perspective, the longhand way is better in my opinion.
10
u/golovatuy Mar 12 '21
let x = 'Hello', y = 55;//Longhandconst temp = x;x = y;y = temp;//Shorthand[x, y] = [y, x];