I am trying to write a Javascript to find the entire row in a table with the highest grades and highlight it. But nothing happens except the table is shown. In this case, the grades are represented by "O" and each "O" represents 5 marks. Below is the coding. Does anyone know the reason?
<html>
<head>
<title> </title>
</head>
<body>
<script language="JavaScript">
var grades=[60, 85, 50];
var names=["Bob", "John", "Mary"];
var i=0;
document.write("<table border='1'>");
document.write("<tr bgcolor='silver'>");
document.write("<td width='100'><strong>Name</strong></td>");
document.write("<td width='200' align='center'><strong>Grade</strong></td>");
document.write("</tr>");
for (i=0; i<grades.length; i++){
document.write("<tr>");
document.write("<td>"+names[i]+"</td>");
document.write("<td>");
for (var j=0; j<grades[i]/5; j++){
document.write(‘O’);
}
document.write("</td>");
document.write("</tr>");
}
function find_Max(){
var max_grades=0;
for(var i=0; i<grades.length;i++){
if (grades[i]>max_grades)
max_grades=grades[i];
}
return max_grades;
}
var max_grades=find_Max(grades[i]);
for(i=0; i<grades.length; i++)
{
rowToColor = grades[i].index
currentRow = getElementsByTagName("max_grades") [rowToColor]
currentRow.bgcolor = “grey”}
find_Max();
document.write("</table>");
</script>
</body>
</html>
2 2999
I am trying to write a Javascript to find the entire row in a table with the highest grades and highlight it. But nothing happens except the table is shown. In this case, the grades are represented by "O" and each "O" represents 5 marks. Below is the coding. Does anyone know the reason?
<html>
<head>
<title> </title>
</head>
<body>
<script language="JavaScript">
var grades=[60, 85, 50];
var names=["Bob", "John", "Mary"];
var i=0;
document.write("<table border='1'>");
document.write("<tr bgcolor='silver'>");
document.write("<td width='100'><strong>Name</strong></td>");
document.write("<td width='200' align='center'><strong>Grade</strong></td>");
document.write("</tr>");
for (i=0; i<grades.length; i++){
document.write("<tr>");
document.write("<td>"+names[i]+"</td>");
document.write("<td>");
for (var j=0; j<grades[i]/5; j++){
document.write(‘O’);
}
document.write("</td>");
document.write("</tr>");
}
function find_Max(){
var max_grades=0;
for(var i=0; i<grades.length;i++){
if (grades[i]>max_grades)
max_grades=grades[i];
}
return max_grades;
}
var max_grades=find_Max(grades[i]);
for(i=0; i<grades.length; i++)
{
rowToColor = grades[i].index
currentRow = getElementsByTagName("max_grades") [rowToColor]
currentRow.bgcolor = “grey”}
find_Max();
document.write("</table>");
</script>
</body>
</html>
Your code was really disorganized - try using comments to make it clear. Also, you were only half using functions and half not. Go one way or the other. Here's what you were getting at I think: -
var grades=[60, 85, 50];
-
var names=["Bob", "John", "Mary"];
-
var max_grades=0;
-
-
//get the max grade index
-
for(var i=0; i<grades.length;i++){
-
if (grades[i]>grades[max_grades])
-
max_grades=i;
-
}
-
-
//write out the table
-
document.write("<table border='1'>");
-
document.write("<tr bgcolor='silver'>");
-
document.write("<td width='100'><strong>Name</strong></td>");
-
document.write("<td width='200' align='center'><strong>Grade</strong></td>");
-
document.write("</tr>");
-
-
for (i=0; i<grades.length; i++){
-
-
//if the row is the max, make it grey
-
if (i == max_grades)
-
document.write("<tr bgcolor='grey'>");
-
else
-
document.write("<tr>");
-
document.write("<td>"+names[i]+"</td>");
-
document.write("<td>");
-
for (var j=0; j<grades[i]/5; j++){
-
document.write("O");
-
}
-
document.write("</td>");
-
document.write("</tr>");
-
}
-
-
document.write("</table>");
-
Your code was really disorganized - try using comments to make it clear. Also, you were only half using functions and half not. Go one way or the other. Here's what you were getting at I think: -
var grades=[60, 85, 50];
-
var names=["Bob", "John", "Mary"];
-
var max_grades=0;
-
-
//get the max grade index
-
for(var i=0; i<grades.length;i++){
-
if (grades[i]>grades[max_grades])
-
max_grades=i;
-
}
-
-
//write out the table
-
document.write("<table border='1'>");
-
document.write("<tr bgcolor='silver'>");
-
document.write("<td width='100'><strong>Name</strong></td>");
-
document.write("<td width='200' align='center'><strong>Grade</strong></td>");
-
document.write("</tr>");
-
-
for (i=0; i<grades.length; i++){
-
-
//if the row is the max, make it grey
-
if (i == max_grades)
-
document.write("<tr bgcolor='grey'>");
-
else
-
document.write("<tr>");
-
document.write("<td>"+names[i]+"</td>");
-
document.write("<td>");
-
for (var j=0; j<grades[i]/5; j++){
-
document.write("O");
-
}
-
document.write("</td>");
-
document.write("</tr>");
-
}
-
-
document.write("</table>");
-
Thanks, b1randon. Yes, that is exactly what i wanted to do... sorry for the disorganise part... your suggestion on using the comments to make the codes clearer is good advice. Javascript became known to me only a couple of months ago...
Sign in to post your reply or Sign up for a free account.
Similar topics
by: megabyte |
last post by:
Hello;
I have an ASP application which gathers information to put in a
database via some forms - I would like to do the form validation
(making sure some fields are not empty) with a Javascript...
|
by: Dav |
last post by:
I am sure that I should be able to send a javascript value (client
side) to a asp.net form via a hidden field. I have seen post where
people say it can be donw but they never post there code.
...
|
by: Tiago Vieira |
last post by:
Hi everybody,
I'm trying to insert a piece of javascript inside of a web service method
but It's not working. Have somebody already tryed to do it?
Folow a piece of my code below.
---------...
|
by: kalaivanan |
last post by:
hi,
How do i pass a javascript value to datagrid itemtemplate textbox using
javascript.
Kalaivanan
|
by: philin007 |
last post by:
Hi ,
I have the following javascript codes:
******************************************
<script language="JavaScript">
<!--
....
.....
if (nextRow >5)
{
|
by: frank78 |
last post by:
Hi all,
I'm having an issue with passing form values from one page to the next.
I wrote a function that I use to change the value of a hidden field to
any number of dynamically generated form...
|
by: Shrek |
last post by:
My xslt file contains a passed parameters
<xsl;param name="Widths">70px,70px,70px,100px,100px</xsl;param>
which I want to use to sets the widths of my table columns .
when I create my...
|
by: mrbadboy |
last post by:
Hi,
I need to access javascript value from flash or i need to access html controls value(like hidden or text box value) from flash. How can i get i from flash. Can you help me how to get it ?
...
|
by: mchen1117 |
last post by:
I try to display a random image alone with text and link, I find this javascript and did some edit as needed, I don't know how I can pass document.write() value to HREF's URL, Please help
...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |