需要 javascript 中的函数来交换数组中的 2 个元素

发布于 2024-08-28 16:20:36 字数 1694 浏览 6 评论 0原文

交换两个数字的程序

        /*
        /*
        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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

生死何惧 2024-09-04 16:20:36

你的代码太长了。如果数组总是包含两个元素,为什么不这样做呢?

function swap(arr) {
    return [arr[1], arr[0]];
}

另外,调用函数的正确方法是:

arr = swap(arr);

如果您希望函数修改其参数,请执行以下操作:

function swap(arr) {
    var tmp = arr[1];
    arr[1] = arr[0];
    arr[0] = tmp;
}

...此外,数组上有一个名为 reverse 的内置方法:

arr.reverse();

Your code is ridiculously long. If the array always contains two elements, why not do this?

function swap(arr) {
    return [arr[1], arr[0]];
}

Also, the correct way to call the function is:

arr = swap(arr);

If you want the function to modify its argument instead, do this instead:

function swap(arr) {
    var tmp = arr[1];
    arr[1] = arr[0];
    arr[0] = tmp;
}

...also, there's a built-in method called reverse on arrays:

arr.reverse();
不即不离 2024-09-04 16:20:36

有点偏离主题,但是这个 Array 方法以最快的方式交换其数组的两个元素(一般情况):

// swaps elements at index i and j in array this
// feature detection for destructuring assignment
Array.prototype.swap = (function () {
    var i=0, j=1;
    try { [i,j]=[j,i]; }
    catch (e) {}
    if(i) {
        return function(i,j) {
            [this[i],this[j]] = [this[j],this[i]];
            return this;
        }
    } else {
        return function(i,j) {
            var temp = this[i];
            this[i] = this[j];
            this[j] = temp;
            return this;
        }
    }
})();

Slighty off topic, but this Array method swaps two elements of its array (general case) by the fastest possible way:

// swaps elements at index i and j in array this
// feature detection for destructuring assignment
Array.prototype.swap = (function () {
    var i=0, j=1;
    try { [i,j]=[j,i]; }
    catch (e) {}
    if(i) {
        return function(i,j) {
            [this[i],this[j]] = [this[j],this[i]];
            return this;
        }
    } else {
        return function(i,j) {
            var temp = this[i];
            this[i] = this[j];
            this[j] = temp;
            return this;
        }
    }
})();
可可 2024-09-04 16:20:36
    function swap(anArray) { 
        var length = anArray.length; 
        var returnArray = new Array(length); 
        for (var i = 0, j = length - 1; i < length; i++, j--) { 
            returnArray[j] = anArray[i]; 
        } 
        return returnArray;
    }
    function swap(anArray) { 
        var length = anArray.length; 
        var returnArray = new Array(length); 
        for (var i = 0, j = length - 1; i < length; i++, j--) { 
            returnArray[j] = anArray[i]; 
        } 
        return returnArray;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文