Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jun 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Code to swap 2 numbers using an array....

    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:

    Code:
    			// 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:

    Code:
    //
    			// 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:

    Code:
    <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

  • #2
    Regular Coder
    Join Date
    Aug 2005
    Location
    Toronto, ON, Canada
    Posts
    231
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    			function swap(swapArray) {
    				
    				var swapArray = new Array (3);
    start fixing from here
    rm -f /

  • #3
    New Coder
    Join Date
    Jun 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    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

  • #4
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Trout69
    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
    Yes, M150 assignment 03 was about 2 months ago (see thread 62055) & this is M150 assignment 05

  • #5
    New Coder
    Join Date
    Jun 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Charlie_90
    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....

  • #6
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Trout69
    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

  • #7
    New Coder
    Join Date
    Jun 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #8
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Charlie is right

    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •