swap() function for variables's values
This question already has an answer here:
Your function is swapping the values internally, but the function does not return a value.
The following method returns an array of values in reverse order of what was supplied.
function swap(x, y) {
var t = x;
x = y;
y = t;
return [x, y];
}
console.log(swap(2, 3));
If you don't actually need the values to swap:
function swap (x, y)
{
return [y, x];
}
If you do need the values to swap, but you don't want to declare another variable:
function swap (x, y)
{
x = x + y;
y = x - y;
x = x - y;
return [x, y];
}
链接地址: http://www.djcxy.com/p/20806.html
下一篇: swap()函数用于变量的值