...

View Full Version : need help with for loop



melCarmasin
05-04-2007, 09:01 AM
I'm trying to loop through 2 arrays and display the largest number and also a name which are both indexed the same. I can't seem to get the loop working properly. If any1 could help, it would be really apreciated. Heres my code.

var markNum = prompt("Enter the number of student marks", "Enter a number");
var i;
var b;
var c;
var inputNames;
var inputMarks;
var names = new Array();
var marks = new Array();
var largeMark;

for (i = 0; i < parseInt(markNum); i++)
{
inputNames = prompt("Enter the names of the sudents");
names[names.length] = inputNames;
}

for (b = 0; b < parseInt(markNum); b++)
{
inputMarks = parseInt(prompt("Enter the mark for " + names));
marks[marks.length] = inputMarks;
}

[B]for (c = 0; c < marks.length; c++)
{
var d = 0;
largeMark = marks[0];
while (d < marks.length)
{
if (largeMark < marks[d])
{
largeMark = marks[d];
}
d++;
}
document.write(names[c] + " has the highest mark. The mark is " + largeMark + "%." + "<br>");
}

venkatmallu.net
05-04-2007, 10:43 AM
i've made some modifications in ur code... its now working right...
i've given some statements where i've made changes...
go through the code. u can copy and paste in ur code.....


<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<script language="JavaScript">
var markNum = prompt("Enter the number of student marks", "Enter a number");
var i;
var b;
var c;
var inputNames;
var inputMarks;
var names = new Array();
var marks = new Array();
var largeMark;
var pos; //take a new variable to find the index of array for highest marks
for (i = 0; i < parseInt(markNum); i++)
{
inputNames = prompt("Enter the names of the sudents");
names[names.length] = inputNames;
}
for (b = 0; b < parseInt(markNum);b++)
{
inputMarks = parseInt(prompt("Enter the mark for " + names[b]));
marks[marks.length] = inputMarks;
}
for (c = 0; c < marks.length; c++)
{
var d = 0;
largeMark = marks[0];
while (d < marks.length)
{
if (largeMark < marks[d])
{
largeMark = marks[d];
pos=d; //assign the value of d to 'pos' at which we get largeMark.
}
d++;
}
}
//place this statement outside of for loop and replace 'names[c]' with 'names[pos]'
document.write(names[pos] + " has the highest mark. The mark is " + largeMark + "%." + "");

</script>
</HEAD>
<BODY>

<P>&nbsp;</P>

</BODY>
</HTML>

glenngv
05-04-2007, 06:03 PM
Alternative solution using a 2D array and the Array.sort function.

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE></TITLE>
<script language="JavaScript">
var markNum = parseInt(prompt("Enter the number of student marks", "Enter a number"));
var i;
var b;
var inputNames;
var inputMarks;
var marks = new Array();

for (i = 0; i < markNum; i++)
{
inputNames = prompt("Enter the names of the students","");
marks[marks.length] = new Array();
marks[marks.length-1][0] = inputNames;
}
for (b = 0; b < markNum;b++)
{
inputMarks = parseInt(prompt("Enter the mark for " + marks[b][0], ""));
marks[b][1] = inputMarks;
}

function sortFunc(a, b){ //reverse sort
return b[1] - a[1];
}

marks.sort(sortFunc);

//the highest mark is in the first element of the array
document.write(marks[0][0] + " has the highest mark. The mark is " + marks[0][1] + "%." + "");

</script>
</HEAD>
<BODY>

<P>&nbsp;</P>

</BODY>
</HTML>

melCarmasin
05-05-2007, 01:04 AM
Thanks guys! Yeah, I thought about using multi-d array but I'm just not very familiar with them just yet.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum