0

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>

6
  • 4
    That is the second most useless generic function I have ever seen. Commented Apr 14, 2010 at 13:44
  • 2
    Anyone else smell homework? ;-) Commented Apr 14, 2010 at 13:46
  • 1
    @eBusiness: I must know the first. Commented Apr 14, 2010 at 13:48
  • 2
    @Andy: yes, "PLACE YOUR CODE FOR THE MAIN PROGRAM HERE" is a dead giveaway. Commented Apr 14, 2010 at 13:48
  • 1
    @Randolpho: Look behind you, an obscure reference to an old PC game! en.wikiquote.org/wiki/Monkey_Island Commented Apr 14, 2010 at 14:08

3 Answers 3

10

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();
Sign up to request clarification or add additional context in comments.

2 Comments

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.
0

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

Comments

0
    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;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.