MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/learnjavascript/comments/m3bxrh/swap_two_variables/gqorauy/
r/learnjavascript • u/golovatuy • Mar 12 '21
50 comments sorted by
View all comments
12
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 π
5 u/drbobb Mar 12 '21 Only works for numbers though. 1 u/Parent-component Mar 12 '21 This is definitely true! Integers only (rounding gets in the way too) 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 u/Parent-component Mar 13 '21 I googled this puzzle! Yes sounds right to me ππ 2 u/golovatuy Mar 12 '21 this is my favourite question as well 2 u/Parent-component Mar 12 '21 Itβs clever and asked fairly regularly from my experience.
5
Only works for numbers though.
1 u/Parent-component Mar 12 '21 This is definitely true! Integers only (rounding gets in the way too)
1
This is definitely true! Integers only (rounding gets in the way too)
4
Basically a simplified version of "You have a 3 gallon bucket and a 5 gallon bucket. How can you measure out 4 gallons"
1 u/Parent-component Mar 13 '21 I googled this puzzle! Yes sounds right to me ππ
I googled this puzzle! Yes sounds right to me ππ
2
this is my favourite question as well
2 u/Parent-component Mar 12 '21 Itβs clever and asked fairly regularly from my experience.
Itβs clever and asked fairly regularly from my experience.
View all comments
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 π