Forum Moderators: open
any help would be appreciated?
Amer
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.
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)
<!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>