需要 javascript 中的函数来交换数组中的 2 个元素
交换两个数字的程序
/*
/*
Function to swap two numbers.
Function takes an argument which is an array of two elements.
Function returns a new array containing the same two elements
as the argument array but in reverse order.
*/
function swap(anArray)
{
// declare and initialise a variable to hold the length of the
argument array
var length = anArray.length;
//declare an array to be returned by the function
var returnArray = new Array(length);
//copy each element of the argument array to the
return array
for (var i = 0; i < length; i = i + 1)
{
returnArray[i] = anArray[i];
}
var anArray [0] = 250;
var anArray [1] = 100;
var tempArray [0] = 0;
var tempArray [1] = 0;
tempArray [0] = anArray [1];
tempArray [1] = anArray [0];
}
document.write('A program to swap two numbers.');
// PLACE YOUR CODE FOR THE MAIN PROGRAM HERE
var anArray = [250,100];
// Display the original array values,
document.write('The original array was ' + anArray[i] + '<BR>');
// invoke swap() with two element array argument
function swap(anArray);
// display final array values
document.write('This array now becomes ' + returnArray[i] + '<BR>');
</SCRIPT>
</HEAD>
<BODY>
</BODY>
A program to swap two numbers
/*
/*
Function to swap two numbers.
Function takes an argument which is an array of two elements.
Function returns a new array containing the same two elements
as the argument array but in reverse order.
*/
function swap(anArray)
{
// declare and initialise a variable to hold the length of the
argument array
var length = anArray.length;
//declare an array to be returned by the function
var returnArray = new Array(length);
//copy each element of the argument array to the
return array
for (var i = 0; i < length; i = i + 1)
{
returnArray[i] = anArray[i];
}
var anArray [0] = 250;
var anArray [1] = 100;
var tempArray [0] = 0;
var tempArray [1] = 0;
tempArray [0] = anArray [1];
tempArray [1] = anArray [0];
}
document.write('A program to swap two numbers.');
// PLACE YOUR CODE FOR THE MAIN PROGRAM HERE
var anArray = [250,100];
// Display the original array values,
document.write('The original array was ' + anArray[i] + '<BR>');
// invoke swap() with two element array argument
function swap(anArray);
// display final array values
document.write('This array now becomes ' + returnArray[i] + '<BR>');
</SCRIPT>
</HEAD>
<BODY>
</BODY>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你的代码太长了。如果数组总是包含两个元素,为什么不这样做呢?
另外,调用函数的正确方法是:
如果您希望函数修改其参数,请执行以下操作:
...此外,数组上有一个名为
reverse
的内置方法:Your code is ridiculously long. If the array always contains two elements, why not do this?
Also, the correct way to call the function is:
If you want the function to modify its argument instead, do this instead:
...also, there's a built-in method called
reverse
on arrays:有点偏离主题,但是这个 Array 方法以最快的方式交换其数组的两个元素(一般情况):
Slighty off topic, but this Array method swaps two elements of its array (general case) by the fastest possible way: