Suppose I was given an array of character strings,
such as ['f', 'r', 'i', 'e', 'n', 'd']
, and my task is to reverse it into ['d', 'n', 'e', 'i', 'r', 'f']
.
I wrote the following JavaScript
var reverseString = function(s) {
let h=0; let t= s.length-1;
while (h<t) {
[s[h], s[t]] = [s[t], s[h]];
h++; t--;
}
};
So the trick I keep using in the while loop is [a,b]=[b,a]
.
How efficient is this in term of space complexity? Is there a better way you would write this in JS? Thank you