Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HTML
<table id="Table">
        <tr>
        <th>id</th>
        <th>Name</th>
        <th>Surname</th>
        <th>school</th>
        <th>score</th>
        </tr>
        <tr>
        <td>1.</td>
        <td>Peter</td>
        <td>B</td>
        <td>primary</td>
        <td>54</td>
        </tr>
        <tr>
        <td>2.</td>
        <td>Mike</td>
        <td>Z</td>
        <td>primary</td>
        <td>42</td>
        </tr>
        <tr>
            <td>3.</td>
            <td>Mia</td>
            <td>M</td>
            <td>primary</td>
            <td>73</td>
            </tr>
            <tr>
                <td>4.</td>
                <td>Jason</td>
                <td>T</td>
                <td>primary</td>
                <td>21</td>
                </tr>
                <tr>
                    <td>5.</td>
                    <td>Jolyn</td>
                    <td>P</td>
                    <td>primary</td>
                    <td>68</td>
                    </tr>
    </table>
<button type="button" onclick="Extract();">Extract </button>
I want to extract rows where score is over 50 on buttton click.

What I have tried:

I tried this solution but it doesn't work for me.
JavaScript
function Extract() {
	var rows = document.getElementsByTagName('tr');
	var cell = document.getElementsByTagName('td');
	for (var i=0; i<cell.length; i++) {
		if (i%5 == 4){
			var x = parseInt(cell[i].innerText);
			if(x<50){for(var j=i-4;j<i+1;j++){
				rows[Math.floor(i/5)+1].innerHTML="";	
				Extract();
				break;}
			}
		}
	}
}
Posted
Updated 24-Sep-22 12:13pm
v2
Comments
Richard Deeming 15-Sep-22 4:47am    
Your code is calling itself over and over again, causing a "too much recursion" error. You need to fix your code.

1 solution

As Richard Deeming suggested your code needs a bit of work. Here's my version of how it should be done.
mySchool = [{Name: "Peter", Surname: "B", school: "primary", score: 54}];
mySchool.push({Name: "Adam", Surname: "B", school: "primary", score: 54});
mySchool.push({Name: "Mike", Surname: "Z", school: "primary", score: 42});
mySchool.push({Name: "Mia", Surname: "M", school: "primary", score: 73});
mySchool.push({Name: "Jason", Surname: "T", school: "primary", score: 21});
mySchool.push({Name: "Jolyn", Surname: "P", school: "primary", score: 68});

myRow = Number(prompt("Enter # "));
mySchool.sort((a, b) => a.score - b.score);

console.table(mySchool); table is sorted in ascending order

/* delete last Row of the array */
mySchool.pop();


P.S. I hope this helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900