Trout69
09-17-2005, 10:25 PM
Hi all....the time has come where being a newbie still to javascript help is needed on a really simple problem. I need to create a function to swap two numbers of an array....i've had a number of different attempts at this....but just keep confusing myself and was hoping for some pointers....i started with something like this:
// A FUNCTION TO SWAP TWO ELEMENTS OF AN ARRAY
function swap(swapArray) {
var swapArray = new Array (3);
swapArray[0] = valueArray[0];
swapArray[1] = valueArray[1];
swapArray[2] = valueArray[0];
valueArray[0] = swapArray[1];
valueArray[1] = swapArray[2];
}
valueArray being an array holding the two values that require to be swapped.
the function i have for testing 'swap' is called 'swapper' as below:
//
// A FUNCTION FOR TESTING THE swap() FUNCTION. IT IS CALLED
// FROM THE HTML BODY AND NEITHER RECEIVES NOR RETURNS A VALUE
//
//
function swapper() {
var valueArray = new Array(2);
valueArray[0] = 10;
valueArray[1] = 20;
swap(valueArray);
window.alert ('The first value is ' + valueArray[0] + ' and the second value is ' +
valueArray[1]);
}
Now, this second funtions does not require any modification. I have called the function swapper but the code is bugged. My full code is as follows:
<HTML>
<HEAD>
<SCRIPT language = "JavaScript">
// A FUNCTION TO SWAP TWO ELEMENTS OF AN ARRAY
function swap(swapArray) {
var swapArray = new Array (3);
swapArray[0] = valueArray[0];
swapArray[1] = valueArray[1];
swapArray[2] = valueArray[0];
valueArray[0] = swapArray[1];
valueArray[1] = swapArray[2];
}
//
// A FUNCTION FOR TESTING THE swap() FUNCTION. IT IS CALLED
// FROM THE HTML BODY AND NEITHER RECEIVES NOR RETURNS A VALUE
//
//
function swapper() {
var valueArray = new Array(2);
valueArray[0] = 10;
valueArray[1] = 20;
swap(valueArray);
window.alert ('The first value is ' + valueArray[0] + ' and the second value is ' +
valueArray[1]);
}
swapper();
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
Now i know this is really simple, but its one of those things that i;ve been staring at for ages and getting no where....lol.
Your thoughts and comments would be most appreciated.
Thanks
Trout
// A FUNCTION TO SWAP TWO ELEMENTS OF AN ARRAY
function swap(swapArray) {
var swapArray = new Array (3);
swapArray[0] = valueArray[0];
swapArray[1] = valueArray[1];
swapArray[2] = valueArray[0];
valueArray[0] = swapArray[1];
valueArray[1] = swapArray[2];
}
valueArray being an array holding the two values that require to be swapped.
the function i have for testing 'swap' is called 'swapper' as below:
//
// A FUNCTION FOR TESTING THE swap() FUNCTION. IT IS CALLED
// FROM THE HTML BODY AND NEITHER RECEIVES NOR RETURNS A VALUE
//
//
function swapper() {
var valueArray = new Array(2);
valueArray[0] = 10;
valueArray[1] = 20;
swap(valueArray);
window.alert ('The first value is ' + valueArray[0] + ' and the second value is ' +
valueArray[1]);
}
Now, this second funtions does not require any modification. I have called the function swapper but the code is bugged. My full code is as follows:
<HTML>
<HEAD>
<SCRIPT language = "JavaScript">
// A FUNCTION TO SWAP TWO ELEMENTS OF AN ARRAY
function swap(swapArray) {
var swapArray = new Array (3);
swapArray[0] = valueArray[0];
swapArray[1] = valueArray[1];
swapArray[2] = valueArray[0];
valueArray[0] = swapArray[1];
valueArray[1] = swapArray[2];
}
//
// A FUNCTION FOR TESTING THE swap() FUNCTION. IT IS CALLED
// FROM THE HTML BODY AND NEITHER RECEIVES NOR RETURNS A VALUE
//
//
function swapper() {
var valueArray = new Array(2);
valueArray[0] = 10;
valueArray[1] = 20;
swap(valueArray);
window.alert ('The first value is ' + valueArray[0] + ' and the second value is ' +
valueArray[1]);
}
swapper();
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
Now i know this is really simple, but its one of those things that i;ve been staring at for ages and getting no where....lol.
Your thoughts and comments would be most appreciated.
Thanks
Trout