...

View Full Version : Code to swap 2 numbers using an array....



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

rm-f
09-17-2005, 10:54 PM
function swap(swapArray) {

var swapArray = new Array (3);
start fixing from here

Trout69
09-17-2005, 11:02 PM
I made a good start then....i'm still working on it (as i a normally am...), javascript seems to be one of those things that i can stare at for ages...then it suddenly hits me what it is....to be honest im about 2 months out of practice, and before this it was only very basic anyway...so here goes.....

Trout :p

Charlie_90
09-21-2005, 11:48 PM
I made a good start then....i'm still working on it (as i a normally am...), javascript seems to be one of those things that i can stare at for ages...then it suddenly hits me what it is....to be honest im about 2 months out of practice, and before this it was only very basic anyway...so here goes.....

Trout :p
Yes, M150 assignment 03 was about 2 months ago (see thread 62055) & this is M150 assignment 05

Trout69
09-22-2005, 09:29 AM
Yes, M150 assignment 03 was about 2 months ago (see thread 62055) & this is M150 assignment 05

Charlie, I don't mean to sound rude, but is it so wrong to discuss issue's your having with an assignment when you are doing a study at home course?

If you were on a campus based course, you could quite freely discuss the assignment with your course buddies....

Charlie_90
09-22-2005, 11:16 AM
is it so wrong to discuss issue's your having with an assignment when you are doing a study at home course?


I think that there is a difference between posting as though this is just a problem you have and posting 'here is an assignment for a course I'm doing, and I can't be bothered to work it out for myself' - people might be a little more reluctant to just bail you out on the latter. Even your mates on a campus based course would get a bit p*ssed off with you if you just expected them to write all your assignments for you

Trout69
09-22-2005, 01:02 PM
I'm not getting into an argument here, as you can see i've asked for pointers...which i have been given? My debugging skills are limited, as is the directions for debugging in the course, i have no where asked for some one to do my assignment. If you check my post for the previous TMA, i am asking for guidance.

The issue is now solved, as you can see not through this post, but by me sitting and staring at the page, so by no means has it been an issue of 'not being bothered'.

I will not be posting in reply to you again, as i find your assumptions insulting. This is a forum at the end of the day, where people should have the freedom to request help if required.

If this is not your purpose then i'm not sure why you have joined?

anonemouse
09-27-2005, 06:27 PM
If it isn't wrong to ask someone to solve your TMA question in a forum, then why not post it in the FC conference?

Another OU student



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum