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>