Presently in this below code, I have 4 dynamic values, end user will be select degree from combo box, entering the values semester, Max marks and Obtained marks as numeric values. Once entered dynamic rows will be created and it also calculate the percentage by the formula (om/mm)*100 for each row on clicking Sum and it also creates new column…

I needed to have code when the degree changes only.. it should calculate the sum of all max.marks and Obtained marks of each semester and then calculate percentage by above formula… and also new column should not be created…..Please sort with max marks… Please help me in this regard… Thank you for helping me initially.

Cheers
Santhosh

``````function insRows()
{
var x = document.getElementById('finalMarks').insertRow();
var cell = x.insertCell(0)
var a = x.insertCell(1);
var b = x.insertCell(2);
var c = x.insertCell(3);
var d = x.insertCell(4);
var element1 = document.createElement("input");
element1.type = "checkbox";
cell.appendChild(element1);
a.innerHTML = document.getElementById('txtqualify').options[document.getElementById('txtqualify').selectedIndex].text
b.innerHTML = document.getElementById('txtsem').value
c.innerHTML = document.getElementById('txtmaxmrk').value
d.innerHTML = document.getElementById('txtobtmrk').value
}

function function1()
{
var myRow = document.all.finalMarks.rows.length;
var ed='';
var mm=0,om=0, x,z;
for(i=0;i<myRow;i++)
{
r = document.all.finalMarks.rows[i];
pr = r.cells[1].innerHTML
mm+= parseInt(r.cells[3].innerHTML)
om+= parseInt(r.cells[4].innerHTML)
if (pr!=ed && ed!='')//(i==myRow-1)
{
prevr = document.all.finalMarks.rows[i-1];
mycurrent_cell = document.createElement("td");
mm-= parseInt(r.cells[3].innerHTML)
om-= parseInt(r.cells[4].innerHTML)
z = (om/mm)*100
mycurrent_cell.innerHTML = z
mm= parseInt(r.cells[3].innerHTML)
om= parseInt(r.cells[4].innerHTML)
alert(mycurrent_cell.innerHTML  +   '['  + pr + ']' + ed)
//alert(!isNaN(mycurrent_cell.innerHTML))
if(!isNaN(mycurrent_cell.innerHTML))
{
qualary[qualary.length] = prevr.cells[1].innerHTML
pctary[pctary.length] = mycurrent_cell.innerHTML
prevr.appendChild(mycurrent_cell);
mycurrent_cell.innerHTML = z;
document.getElementById('txtpert').value = z
}
}
ed = r.cells[1].innerHTML
alert(ed)
if (i==myRow-1)
{
prevr = document.all.finalMarks.rows[i];
//mycurrent_cell = document.createElement("td");
x = (om/mm)*100
mycurrent_cell.innerHTML = x
qualary[qualary.length] = prevr.cells[1].innerHTML
pctary[pctary.length] = mycurrent_cell.innerHTML
prevr.appendChild(mycurrent_cell);
if(!isNaN(x))
document.getElementById('txtperct').value = x
}
}
//document.frmMarks.txtper.value = Math.max( x, z)
var str = sortAndReturn(pctary,qualary)
//alert(pctary  +   '\$'   + qualary)    //alert(sortAndReturn(pctary,qualary))
//alert(str)    //alert(str[0]+','+str[1])
document.getElementById('degr').value = str.split('\$')[0]
document.getElementById('txtmarks').value = str.split('\$')[1]
}
function sortAndReturn(pctary,qualary)
{
var myArray3 = new Array();
for(i=0;i<pctary.length;i++)
myArray3[i]=pctary[i];
sort(pctary)
//alert(sort(pctary))    //alert(myArray3)
return qualary[indexSearch(myArray3,pctary[0])] + '\$' + pctary[0]
}
function sort(a)
{
var  max;
for(i=0;i<a.length;i++)
{
max   =  i;
for(j=i+1;j<a.length;j++)
{
if (a[j]>a[max])
{
max=j;
}
}
//alert(a[max])
if (i != max)
{
var swap = a[i];
a[i] = a[max];
a[max] = swap;
}
}

}
function indexSearch(a,b)
{
for(i=0;i<a.length;i++)
if(a[i]==b)
{return i ;}
}``````

HTML CODE:

``````<table BORDER ="6"  width="85%"align="center" bgcolor="#00FFFF"  style="font-family: Verdana;color: black;font-size: 12px">
<tr>
<td align="center" width="25%" height="40"> Qualification:</td>
<td align="left" style="WIDTH:25%" >
<select id ="txtqualify" style ="width:150px;height:25px" name ="txtqualify"  > </select> </td>
<td align="center" width="25%" height="40">Semester:</td>
<td> <input type = "text" SIZE="8" maxlength = "3" id = "txtsem" name = "txtsem"> </td>
</tr>
<tr>
<td align= "center" width ="25%" height="40">Max.Marks:</td>
<td> <input type = "text" SIZE="8" maxlength = "3" id = "txtmaxmrk" name = "txtmaxmrk"> </td>
<td align= "center" width ="25%" height="40">Obtained.Marks:</td>
<td> <input type = "text" SIZE="8" maxlength = "3" id = "txtobtmrk" name = "txtobtmrk"> </td>
</tr>
<tr>
<td align= "center" width ="25%" height="40">Percentage:</td>
<td> <input type = "text" SIZE="8" maxlength = "5" id = "txtper" readonly name = "txtper">
<td align="center" colspan="4" valign="middle">
<input type="RESET" align="middle" style="width:20%" value="ADD"  onclick="insRows()" >
<input type="button" align="middle" style="width:20%" value="REMOVE" onclick="delRow()" >
<input type="button" align="middle" style="width:20%" value="CLOSE" onclick="" >
<input type="button" align="middle" style="width:20%" value="SUM" onclick="function1()" >
</td>
</tr>
<tr>
<td  align="center" height="40"> <input type = "text" SIZE="8" maxlength = "5" id = "txtpert" name = "txtpert"> </td>
<td align="center"> <input type = "text" SIZE="8" maxlength = "5" id = "txtperct" name = "txtperct"> </td>
<td align="center"> <input type = "text" SIZE="10" maxlength = "5" readonly id = "degr" name = "degr"> </td>
</tr>
</table>
<table id ="finalMarks" BORDER ="6" width="85%"align="center" bgcolor="#00FFFF"  style="font-family: Verdana;color: black;font-size: 12px">
<thead>
<th height="40">Remove</th>
<th>Qualification</th>
<th>Sem/Year</th>
<th>Max.Marks</th>
<th>Obt.Marks</th>
<th>Percentage of Marks:</th>
</thead>
</table>``````

## All 7 Replies

Not sure if this code works on IE because of insertRow() function... I do not have time to show you right now. Will try to get to it once I have some times. Though, one comment on 'indexSearch' function. You should do binary search for that because the array is sorted. It would be a waste to do linear indexSearch in a sorted array.

I just reread your code & explanation again, it is a bit complicated issue here. If I were you, I would not create the dynamic part as a table but rather div elements. The reason is that IE does not support dynamic table row/cells well. Your code is mixed between old IE and standard js. However, there is a downside for using div that is the display. In order to force the display to be similar to a table, you need to use a lot of style (CSS).

If you are still interested in fixing this up, please reply; otherwise, I would not spend time writing a sample of this snippet. There are a lot of things going on in your code even though you want to update data when change degree only. (insert row is one part, remove row is the second part, and computing is the third part.)

Presently in this below code, I have 4 dynamic values, end user will be select degree from combo box, entering the values semester, Max marks and Obtained marks as numeric values. Once entered dynamic rows will be created and it also calculate the percentage by the formula (om/mm)*100 for each row on clicking Sum and it also creates new column…

I needed to have code when the degree changes only.. it should calculate the sum of all max.marks and Obtained marks of each semester and then calculate percentage by above formula… and also new column should not be created…..Please sort with max marks… Please help me in this regard… Thank you for helping me initially.

Cheers
Santhosh

``````function insRows()
{
var x = document.getElementById('finalMarks').insertRow();
var cell = x.insertCell(0)
var a = x.insertCell(1);
var b = x.insertCell(2);
var c = x.insertCell(3);
var d = x.insertCell(4);
var element1 = document.createElement("input");
element1.type = "checkbox";
cell.appendChild(element1);
a.innerHTML = document.getElementById('txtqualify').options[document.getElementById('txtqualify').selectedIndex].text
b.innerHTML = document.getElementById('txtsem').value
c.innerHTML = document.getElementById('txtmaxmrk').value
d.innerHTML = document.getElementById('txtobtmrk').value
}

function function1()
{
var myRow = document.all.finalMarks.rows.length;
var ed='';
var mm=0,om=0, x,z;
for(i=0;i<myRow;i++)
{
r = document.all.finalMarks.rows[i];
pr = r.cells[1].innerHTML
mm+= parseInt(r.cells[3].innerHTML)
om+= parseInt(r.cells[4].innerHTML)
if (pr!=ed && ed!='')//(i==myRow-1)
{
prevr = document.all.finalMarks.rows[i-1];
mycurrent_cell = document.createElement("td");
mm-= parseInt(r.cells[3].innerHTML)
om-= parseInt(r.cells[4].innerHTML)
z = (om/mm)*100
mycurrent_cell.innerHTML = z
mm= parseInt(r.cells[3].innerHTML)
om= parseInt(r.cells[4].innerHTML)
alert(mycurrent_cell.innerHTML  +   '['  + pr + ']' + ed)
//alert(!isNaN(mycurrent_cell.innerHTML))
if(!isNaN(mycurrent_cell.innerHTML))
{
qualary[qualary.length] = prevr.cells[1].innerHTML
pctary[pctary.length] = mycurrent_cell.innerHTML
prevr.appendChild(mycurrent_cell);
mycurrent_cell.innerHTML = z;
document.getElementById('txtpert').value = z
}
}
ed = r.cells[1].innerHTML
alert(ed)
if (i==myRow-1)
{
prevr = document.all.finalMarks.rows[i];
//mycurrent_cell = document.createElement("td");
x = (om/mm)*100
mycurrent_cell.innerHTML = x
qualary[qualary.length] = prevr.cells[1].innerHTML
pctary[pctary.length] = mycurrent_cell.innerHTML
prevr.appendChild(mycurrent_cell);
if(!isNaN(x))
document.getElementById('txtperct').value = x
}
}
//document.frmMarks.txtper.value = Math.max( x, z)
var str = sortAndReturn(pctary,qualary)
//alert(pctary  +   '\$'   + qualary)    //alert(sortAndReturn(pctary,qualary))
//alert(str)    //alert(str[0]+','+str[1])
document.getElementById('degr').value = str.split('\$')[0]
document.getElementById('txtmarks').value = str.split('\$')[1]
}
function sortAndReturn(pctary,qualary)
{
var myArray3 = new Array();
for(i=0;i<pctary.length;i++)
myArray3[i]=pctary[i];
sort(pctary)
//alert(sort(pctary))    //alert(myArray3)
return qualary[indexSearch(myArray3,pctary[0])] + '\$' + pctary[0]
}
function sort(a)
{
var  max;
for(i=0;i<a.length;i++)
{
max   =  i;
for(j=i+1;j<a.length;j++)
{
if (a[j]>a[max])
{
max=j;
}
}
//alert(a[max])
if (i != max)
{
var swap = a[i];
a[i] = a[max];
a[max] = swap;
}
}

}
function indexSearch(a,b)
{
for(i=0;i<a.length;i++)
if(a[i]==b)
{return i ;}
}``````

HTML CODE:

``````<table BORDER ="6"  width="85%"align="center" bgcolor="#00FFFF"  style="font-family: Verdana;color: black;font-size: 12px">
<tr>
<td align="center" width="25%" height="40"> Qualification:</td>
<td align="left" style="WIDTH:25%" >
<select id ="txtqualify" style ="width:150px;height:25px" name ="txtqualify"  > </select> </td>
<td align="center" width="25%" height="40">Semester:</td>
<td> <input type = "text" SIZE="8" maxlength = "3" id = "txtsem" name = "txtsem"> </td>
</tr>
<tr>
<td align= "center" width ="25%" height="40">Max.Marks:</td>
<td> <input type = "text" SIZE="8" maxlength = "3" id = "txtmaxmrk" name = "txtmaxmrk"> </td>
<td align= "center" width ="25%" height="40">Obtained.Marks:</td>
<td> <input type = "text" SIZE="8" maxlength = "3" id = "txtobtmrk" name = "txtobtmrk"> </td>
</tr>
<tr>
<td align= "center" width ="25%" height="40">Percentage:</td>
<td> <input type = "text" SIZE="8" maxlength = "5" id = "txtper" readonly name = "txtper">
<td align="center" colspan="4" valign="middle">
<input type="RESET" align="middle" style="width:20%" value="ADD"  onclick="insRows()" >
<input type="button" align="middle" style="width:20%" value="REMOVE" onclick="delRow()" >
<input type="button" align="middle" style="width:20%" value="CLOSE" onclick="" >
<input type="button" align="middle" style="width:20%" value="SUM" onclick="function1()" >
</td>
</tr>
<tr>
<td  align="center" height="40"> <input type = "text" SIZE="8" maxlength = "5" id = "txtpert" name = "txtpert"> </td>
<td align="center"> <input type = "text" SIZE="8" maxlength = "5" id = "txtperct" name = "txtperct"> </td>
<td align="center"> <input type = "text" SIZE="10" maxlength = "5" readonly id = "degr" name = "degr"> </td>
</tr>
</table>
<table id ="finalMarks" BORDER ="6" width="85%"align="center" bgcolor="#00FFFF"  style="font-family: Verdana;color: black;font-size: 12px">
<thead>
<th height="40">Remove</th>
<th>Qualification</th>
<th>Sem/Year</th>
<th>Max.Marks</th>
<th>Obt.Marks</th>
<th>Percentage of Marks:</th>
</thead>
</table>``````

Hi Taywin,
Thank you for replying..
I request to provide code for sorting and to find max no in an dynamic array if possible to sort it out this problem

Regards,
Santhosh

Santhosh:

OK, the code below should work the way you want. If not, please let me know. Also, please read what I listed right below here for reasons and explanations of the code before you try my code.

1) Your code would work only in IE. I modified it to be cross-browser (can use in any browser if not all).
2) The 'txtmarks' element does not exist. I guess you have a typo and want 'txtpert' to be 'txtmarks'. The reason is that you already have 'txtperct' which shows the percentage of overall.
3) You are overly complicated yourself when you do the computation. You do not need to sort the data in order to find its total obtained mark, percentage, and semester. What you need to do is to iterate through the table once. While you are going through the table, save all the value you are going through in variables & use those values to display. Unless you want to sort the display table below, that would require sort and swap element values.
4) You should always use local variable for your loop. It is very dangerous to use variable without declaring as local because the variable will be assumed as global. In other words, the value could be changed anywhere else by any function. Use 'var' to declare the variable as local where you want the variable to stay alive.
5) I am not sure you want the total obtained marks or max obtain mark for your 'txtmarks'. The default is to display total obtained marks, but you could delete it and uncomment the line that display max obtained mark instead.
6) The code does NOT have delRow() implemented because you do not have it in there.
7) The code does NOT validate the value of obtained mark. In other words, if obtained mark is higher than max mark, nothing is checked or display any error.

Javascript code

``````function insRows() {
var insertedTable = document.getElementById('finalMarks');
var x;
var c2Val = document.getElementById('txtsem').value
var c3Val = parseInt(document.getElementById('txtmaxmrk').value,10)
var c4Val = parseInt(document.getElementById('txtobtmrk').value,10)
if (c2Val.length<1 || c3Val<=0 || c4Val<=0) {
alert("Invalid semester, max mark, or obtain mark value");
return;  // not add if any of values is invalid
}
if (document.all) { // IE
x = document.getElementById('finalMarks').insertRow();
}
else {  // other browsers
x = document.getElementById('finalMarks').insertRow(insertedTable.getElementsByTagName("tr").length);
}
var cell0 = x.insertCell(0)
var cell1 = x.insertCell(1);
var cell2 = x.insertCell(2);
var cell3 = x.insertCell(3);
var cell4 = x.insertCell(4);
var cell5 = x.insertCell(5);
var element1 = document.createElement("input");
element1.type = "checkbox";
cell0.appendChild(element1);
cell1.innerHTML = document.getElementById('txtqualify').options[document.getElementById('txtqualify').selectedIndex].text;
cell2.innerHTML = c2Val;
cell3.innerHTML = c3Val;
cell4.innerHTML = c4Val;
cell5.innerHTML = (c4Val/c3Val)*100 + "%";
}

function function1()
{
var myTable = document.getElementById("finalMarks");
var selDegree = document.getElementById('txtqualify').options[document.getElementById('txtqualify').selectedIndex].text
var myRows = myTable.getElementsByTagName("tr");  // should work in both IE and others
var mm=0, om=0, row, cells, maxOm=0;
for (var i=1; i<myRows.length; i++) {  // caution! should declare 'i' as local variable
row = myRows[i];  // obtain the table row element
cells = row.getElementsByTagName("td");
if (selDegree==cells[1].innerHTML) {  // the row degree is the same as selected
mm += parseInt(cells[3].innerHTML, 10);  // cumulate max marks
om += parseInt(cells[4].innerHTML, 10);  // cumulate obtained marks
if (om>maxOm) { maxOm = om; } // get max value of obtained mark
}
}
document.getElementById('txtperct').value = (om/mm)*100 + "%";
document.getElementById('degr').value = selDegree;
document.getElementById('txtmarks').value = om;  // write total obtain marks
}``````

HTML portion

``````<table BORDER ="6"  width="85%"align="center" bgcolor="#00FFFF"  style="font-family: Verdana;color: black;font-size: 12px">
<tr>
<td align="center" width="25%" height="40"> Qualification:</td>
<td align="left" style="WIDTH:25%" >
<select id ="txtqualify" style ="width:150px;height:25px" name ="txtqualify">
<option value=1>Undergrade</option>
<option value=2>Master</option>
</select> </td>
<td align="center" width="25%" height="40">Semester:</td>
<td> <input type = "text" SIZE="8" maxlength = "3" id = "txtsem" name = "txtsem"> </td>
</tr>
<tr>
<td align= "center" width ="25%" height="40">Max.Marks:</td>
<td> <input type = "text" SIZE="8" maxlength = "3" id = "txtmaxmrk" name = "txtmaxmrk"> </td>
<td align= "center" width ="25%" height="40">Obtained.Marks:</td>
<td> <input type = "text" SIZE="8" maxlength = "3" id = "txtobtmrk" name = "txtobtmrk"> </td>
</tr>
<tr>
<td align= "center" width ="25%" height="40">Percentage:</td>
<td> <input type = "text" SIZE="8" maxlength = "5" id = "txtper" readonly name = "txtper">
<td align="center" colspan="4" valign="middle">
<input type="RESET" align="middle" style="width:20%" value="ADD"  onclick="insRows()" >
<input type="button" align="middle" style="width:20%" value="REMOVE" onclick="delRow()" >
<input type="button" align="middle" style="width:20%" value="CLOSE" onclick="" >
<input type="button" align="middle" style="width:20%" value="SUM" onclick="function1()" >
</td>
</tr>
<tr>
<td  align="center" height="40"> <input type = "text" SIZE="8" maxlength = "5" id = "txtmarks" name = "txtmarks"> </td>
<td align="center"> <input type = "text" SIZE="8" maxlength = "5" id = "txtperct" name = "txtperct"> </td>
<td align="center"> <input type = "text" SIZE="10" maxlength = "5" readonly id = "degr" name = "degr"> </td>
</tr>
</table>
<table id ="finalMarks" BORDER ="6" width="85%"align="center" bgcolor="#00FFFF"  style="font-family: Verdana;color: black;font-size: 12px">
<th height="40">Remove</th>
<th>Qualification</th>
<th>Sem/Year</th>
<th>Max.Marks</th>
<th>Obt.Marks</th>
<th>Percentage of Marks:</th>
</table>``````

Hi Taywin,

Presently you had provided code for adding only one degree…. I like to have the code for number of degrees and their marks will be added, in that max percentage and its degree to be stored..

And how to validate the value of Obtained mark… I.e.… if obtained mark is higher than max mark how to display an error.

How to go ahead.. Please help in this regard..

Regards,
Santhosh

Hi Taywin,

Please help me in this regards....... its urgent...

Regards,
Santhosh

Hi Taywin,

Please help me in this regards....... its urgent...

Regards,
Santhosh

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.