views:

610

answers:

3

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>

+7  A: 

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(ass) {
    var tmp = arr[1];
    arr[1] = arr[0];
    arr[0] = tmp;
}

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

arr.reverse();
Matti Virkkunen
That helped a bit - now I need to work on the output, as it isn't giving me the array.Ta!
From now on, please tag your homework questions a homework so I don't accidentally answer them.
Matti Virkkunen
@Matti: I thought you had enough +1s until I read that comment lol.
Andy E
A: 

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;
        }
    }
})();
Alsciende
A: 
    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;
    }