DEV Community

Discussion on: Terrible interview question: Swap variables without a temporary

Collapse
 
diek profile image
diek • Edited

This question is really a pain.

I tried with javascript, the multiassign doesn't work, at least in chrome.

I solved it with the deconstruct operator:

let a = 1;
let b = 2;
[a,b] = [b,a];

It does the trick, but knowing how to do this in an interview wouldn't be my main concern if i was the interviewer.

Collapse
 
karataev profile image
Eugene Karataev

This solution looks good, but it doesn't qualify for the requirements, because you create a temporary array [b,a] in the third line.