DekGenius.com
JAVASCRIPT
swap in javascript
var first = 5;
var second = 7;
[first, second] = [second, first];
console.log(first, second);
//answer - 7 5
swap function javascript
function swap(x, y) {
var t = x;
x = y;
y = t;
return [x, y];
}
console.log(swap(2, 3));
javascript swap variables
var a = 5;
var b = 3;
[a, b] = [b, a];
Swapping variables JS
let a = 1;
let b = 3;
[a, b] = [b, a];
console.log(a); // 3
console.log(b); // 1
const arr = [1,2,3];
[arr[2], arr[1]] = [arr[1], arr[2]];
console.log(arr); // [1,3,2]
swap function javascript
let a = 1;
let b = 2;
let temp;
temp = a;a = b;b = temp;
a; // => 2
b; // => 1
how do you swap the vaRIables js
let a = "red";
let b = "blue";
let c = a; // red
a = b; //over-rides to blue
b = c;
console.log(a);
console.log(b);
Javascript swap old and new method
//old method
var a = 1;
var b = 2;
var temp = a;
a = b;
b = temp;
console.log(a, b)
//expected output: 2 1
//new method(using destructuring method for swap)
var x = 10;
var y = 20;
[y, x] = [x, y];
console.log(y, x);
//expected output: y = 10 and x = 20
swap function javascript
let a = 1;
let b = 2;
a = a ^ b;b = a ^ b;a = a ^ b;
a; // => 2
b; // => 1
swap function javascript
let a = 1;
let b = 2;
a = a + b;b = a - b;a = a - b;
a; // => 2
b; // => 1
swap function javascript
let a = 1;
let b = 2;
[a, b] = [b, a];
a; // => 2
b; // => 1
swap function javascript
function swap(x, y) {
return [y, x];
}
console.log(swap(2, 3));
swap in javascript
var first = 5;
var second = 7;
[first, second] = [second, first];
console.log(first, second);
//answer - 7 5
swap function javascript
function swap(x, y) {
var t = x;
x = y;
y = t;
return [x, y];
}
console.log(swap(2, 3));
javascript swap variables
var a = 5;
var b = 3;
[a, b] = [b, a];
Swapping variables JS
let a = 1;
let b = 3;
[a, b] = [b, a];
console.log(a); // 3
console.log(b); // 1
const arr = [1,2,3];
[arr[2], arr[1]] = [arr[1], arr[2]];
console.log(arr); // [1,3,2]
swap function javascript
let a = 1;
let b = 2;
let temp;
temp = a;a = b;b = temp;
a; // => 2
b; // => 1
how do you swap the vaRIables js
let a = "red";
let b = "blue";
let c = a; // red
a = b; //over-rides to blue
b = c;
console.log(a);
console.log(b);
Javascript swap old and new method
//old method
var a = 1;
var b = 2;
var temp = a;
a = b;
b = temp;
console.log(a, b)
//expected output: 2 1
//new method(using destructuring method for swap)
var x = 10;
var y = 20;
[y, x] = [x, y];
console.log(y, x);
//expected output: y = 10 and x = 20
swap function javascript
let a = 1;
let b = 2;
a = a ^ b;b = a ^ b;a = a ^ b;
a; // => 2
b; // => 1
swap function javascript
let a = 1;
let b = 2;
a = a + b;b = a - b;a = a - b;
a; // => 2
b; // => 1
swap function javascript
let a = 1;
let b = 2;
[a, b] = [b, a];
a; // => 2
b; // => 1
swap function javascript
function swap(x, y) {
return [y, x];
}
console.log(swap(2, 3));
© 2022 Copyright:
DekGenius.com