Results 1 to 6 of 6

Thread: Javscript struggle

  1. #1

    Thread Starter
    Lively Member amer7862000's Avatar
    Join Date
    Apr 2004
    Location
    North West, UK
    Posts
    94

    Resolved Javscript struggle

    Hi,
    Can any1 help me with a little javascripting; The scenario is:
    There are four modules.. 1.Database 2.Network 3.Project 4.Programming.. what I want the script to do is select only the three best marks out of four.. i.e.
    If a student gained:
    Database: 50
    Network: 60
    Project: 10
    Programming: 40
    Then the script would only select: 50 + 60 + 40..
    can this actually be done on javascript?
    any help would be appreciated?

    Amer
    Last edited by amer7862000; Oct 11th, 2004 at 03:45 PM.
    "Through every dark night there's a brighter day, so no matter how hard it get, put your chest out and keep your head up, and handle it"

  2. #2
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    Here's one method. Get the input (I'd use a form). Then store the data in an array. then do a bubble sort on the array. Then output the last 3 items in the array. I'll get onto coding it now if you want.
    Have I helped you? Please Rate my posts.

  3. #3
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    Here you go:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Bubble sort</title>
    <script type="text/javascript">
    function doIt() {
    	score = new Array()
    	score[0] = document.getElementById('scoreDB').value
    	score[1] = document.getElementById('scoreNW').value
    	score[2] = document.getElementById('scorePR').value
    	score[3] = document.getElementById('scorePG').value
    	BubbleSort(score)
    	document.getElementById('answer').innerHTML = score[1]+"<br />"+score[2]+"<br />"+score[3]
    }
    
    function Bubble(list) {
    	for (i=0; i<11; i++)
    	{
    	if (list[i] > list[1+i])
    		{
    		swap(list,i,i+1);
    		}
    	}
    }
    
    function BubbleSort(list) {
    	for (j=0; j<list.length; j++)
    		{
    		Bubble(list);
    		}
    }
    
    function swap(list,x,y) {
    	temp = list[x];
    	list[x] = list[y];
    	list[y] = temp;
    }
    </script>
    </head>
    
    <body>
    <label>Database score:<input type="text" id="scoreDB" /></label><br />
    <label>Network score:<input type="text" id="scoreNW" /></label><br />
    <label>Project score:<input type="text" id="scorePR" /></label><br />
    <label>Programming score:<input type="text" id="scorePG" /></label><br />
    <input type="button" value="Select best ones" onclick="doIt()" /><br />
    <span id="answer"></span>
    </body>
    </html>
    Have I helped you? Please Rate my posts.

  4. #4

    Thread Starter
    Lively Member amer7862000's Avatar
    Join Date
    Apr 2004
    Location
    North West, UK
    Posts
    94
    Cheers Acid ....
    That was exactly wat i was looking for!...
    "Through every dark night there's a brighter day, so no matter how hard it get, put your chest out and keep your head up, and handle it"

  5. #5
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    Acidic...

    Just for info purposes: (I think) Javascript arrays have a .Sort() method which is faster than bubble sort (I think) although in this case its probably negligible.


    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  6. #6
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    aah, very true. I've never actually used that function though. I just remember fixing the bubble sort alog for some guy a while back and copy pasted and edited it in.
    Have I helped you? Please Rate my posts.

Posting Permissions

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



Click Here to Expand Forum to Full Width