Forum Moderators: open

Message Too Old, No Replies

javascript struggle....!

selecting 3 marks out of 4...

         

amer7862000

9:15 pm on Oct 6, 2004 (gmt 0)

10+ Year Member



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

Bernard Marx

9:52 pm on Oct 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There should be a more generic way (and more efficient),
but for the moment this works anyway.
It copies an array an then removes the least member.
You'll need 2 (otherwise useful) custom methods:

Array.prototype.min = function()
{
var min = this[0]
for(var k=1;k<this.length;k++)
min = Math.min(min,this[k])
return min;
}

Array.prototype.indexOf = function(test,i)
{
for(i = i¦¦0;i<this.length;i++)
if(this[i]==test)
return i;
return -1
}

arr = [9,2,4,6]
// *demo of constituents
// copy = arr.slice() // so as not to mess up the original
// copy.min() --> 2
// copy.indexOf(2) --> 1
// copy.splice(1,1) // removes 1th elm (+returns it)

// all together
copy = arr.slice()
copy.splice(copy.indexOf(copy.min()),1)

alert('['+copy+']') // --> [9,4,6]

*! Replace ¦¦ with unbroken pipes, in 'indexOf' method.

Bernard Marx

11:12 pm on Oct 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I wasn't happy with that. OK, but sloppy.
What if you wanted the best 3 out of 5? - it wouldn't do it.
Although there's maybe a small efficiency loss involved, using a basic sorting technique, but only bothering with the first n-many is probaly good. You get your results sorted too! This was the best I could do this evening:

Array.prototype.highestValueSubset = function(length)
{
var copy = this.slice(), i=-1,j,temp,t;
while(++i<length)
{
temp = copy[t=i];
for(j=i+1;j<copy.length;j++)
if(temp<copy[j])
temp = copy[t=j];
copy[t] = copy[i];
copy[i] = temp;
}
copy.length = length;
return copy;
}

// eg:
arr = [2,4,9,9,6,8,5]
sub = arr.highestValueSubset(3)
alert(sub)

Rambo Tribble

1:35 am on Oct 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Err, why not just use Array.sort()? You can pass a function to the method to control the sort order. To quote from Flanagan:

function numberorder(a,b){return a-b;}

a=new Array(33,4,1111,222)
a.sort(numberorder); //Numerical sort: 4, 33, 222, 1111 (reverse the a-b to b-a to get a high-to-low sort)

Rambo Tribble

4:10 am on Oct 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's a q'n'd example using sort():


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function numberOrder(a,b){return a-b;}
function showScores(frm){
var arr=new Array();
var tell="";
for(var i=0;i<frm.length-1;i++){ //form length -1 drops submit button
arr[i]=parseInt(frm.elements[i].value);
}
var ln=arr.length;
arr.sort(numberOrder);
for(var i=ln-1;i>ln-4;i--){
tell += arr[i]+" ";
}
alert(tell);
}
</script>
</head>
<body>
<form action="">
<p>
Database: <input type="text" value="" /><br />
Network: <input type="text" value="" /><br />
Project: <input type="text" value="" /><br />
Programming: <input type="text" value="" /><br />
<input type="submit" value="Show Highs" onclick="showScores(this.form);return false;" />
</p>
</form>
</body>
</html>

Bernard Marx

7:50 am on Oct 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh yes, there is that!
Must have been too late at night.
Was fun though.