473,385 Members | 1,546 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Javascript:Finding the max value inside a table row & highlight

12
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>
Jan 9 '07 #1
2 2999
b1randon
171 Expert 100+
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:
Expand|Select|Wrap|Line Numbers
  1. var grades=[60, 85, 50];
  2. var names=["Bob", "John", "Mary"];
  3. var max_grades=0;
  4.  
  5. //get the max grade index
  6. for(var i=0; i<grades.length;i++){
  7.     if (grades[i]>grades[max_grades])
  8.         max_grades=i;
  9. }
  10.  
  11. //write out the table
  12. document.write("<table border='1'>");
  13. document.write("<tr bgcolor='silver'>");
  14. document.write("<td width='100'><strong>Name</strong></td>");
  15. document.write("<td width='200' align='center'><strong>Grade</strong></td>");
  16. document.write("</tr>");
  17.  
  18. for (i=0; i<grades.length; i++){
  19.  
  20.     //if the row is the max, make it grey
  21.     if (i == max_grades)
  22.         document.write("<tr bgcolor='grey'>");
  23.     else
  24.         document.write("<tr>");
  25.     document.write("<td>"+names[i]+"</td>");
  26.     document.write("<td>");
  27.     for (var j=0; j<grades[i]/5; j++){
  28.         document.write("O");
  29.     }
  30.     document.write("</td>");
  31.     document.write("</tr>");
  32. }
  33.  
  34. document.write("</table>");
  35.  
Jan 9 '07 #2
Pete90
12
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:
Expand|Select|Wrap|Line Numbers
  1. var grades=[60, 85, 50];
  2. var names=["Bob", "John", "Mary"];
  3. var max_grades=0;
  4.  
  5. //get the max grade index
  6. for(var i=0; i<grades.length;i++){
  7.     if (grades[i]>grades[max_grades])
  8.         max_grades=i;
  9. }
  10.  
  11. //write out the table
  12. document.write("<table border='1'>");
  13. document.write("<tr bgcolor='silver'>");
  14. document.write("<td width='100'><strong>Name</strong></td>");
  15. document.write("<td width='200' align='center'><strong>Grade</strong></td>");
  16. document.write("</tr>");
  17.  
  18. for (i=0; i<grades.length; i++){
  19.  
  20.     //if the row is the max, make it grey
  21.     if (i == max_grades)
  22.         document.write("<tr bgcolor='grey'>");
  23.     else
  24.         document.write("<tr>");
  25.     document.write("<td>"+names[i]+"</td>");
  26.     document.write("<td>");
  27.     for (var j=0; j<grades[i]/5; j++){
  28.         document.write("O");
  29.     }
  30.     document.write("</td>");
  31.     document.write("</tr>");
  32. }
  33.  
  34. document.write("</table>");
  35.  
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...
Jan 10 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
1
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. ...
1
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. ---------...
1
by: kalaivanan | last post by:
hi, How do i pass a javascript value to datagrid itemtemplate textbox using javascript. Kalaivanan
4
by: philin007 | last post by:
Hi , I have the following javascript codes: ****************************************** <script language="JavaScript"> <!-- .... ..... if (nextRow >5) {
4
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...
1
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...
3
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 ? ...
1
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 ...
0
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...
0
isladogs
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...
0
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...
0
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,...
0
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$) { } ...
0
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...
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.