0
function myFunction12(){
    var table1 = document.getElementById("table1").rows; 
    var y31;
    for(var i = 2; i < table1.length; i++) {
        for(var j = 4; j < 5; j++) {
            y31 = table1[i].cells;
            if((y31[j].innerText)>="79 %") {
                y31[j].style.backgroundColor ="red";
            } else if(((y31[j].innerText)>="80 %") && ((y31[j].innerText)<="89 %")) {
                y31[j].style.backgroundColor ="YELLOW";
            } else if((y31[j].innerText)>="90 %") {
                y31[j].style.backgroundColor ="GREEN";
            }
        }
    }
}

The above jscript selects a table value and highlights it if a condition is satisfied, but as per define condition highlighting in correct cell.

function myFunction12(){
    var table1 = document.getElementById("table1").rows; 
    var y31;
    for(var i = 2; i < table1.length; i++) {
        for(var j = 4; j < 5; j++) {
            y31 = table1[i].cells;
            if((y31[j].innerText)>="79 %") {
                y31[j].style.backgroundColor ="red";
            } else if(((y31[j].innerText)>="80 %") && ((y31[j].innerText)<="89 %")) {
            y31[j].style.backgroundColor ="YELLOW";
        } else if((y31[j].innerText)>="90 %") {
            y31[j].style.backgroundColor ="GREEN";}
        }
    } 
}
1
  • I have never dealt with this technology before but perhaps the space between "79 or "80 and "90 and the %" is problematic? Commented Feb 22, 2023 at 20:37

1 Answer 1

0

Here is the working suggestion:

function myFunction12() {
    var table1 = document.getElementById("table1").rows;
    var y31;
    for (var i = 2; i < table1.length; i++) {
        for (var j = 0; j < table1[i].cells.length; j++) {
            y31 = table1[i].cells;
            if ((y31[j].innerText) >= 79) {
                y31[j].style.backgroundColor = "red";
            } if (((y31[j].innerText) >= 80) && ((y31[j].innerText) <= 89)) {
                y31[j].style.backgroundColor = "yellow";
            } if ((y31[j].innerText) >= 90) {
                console.log(y31[j].innerText, (y31[j].innerText) >= 90)
                y31[j].style.backgroundColor = "green";
            }
        }
    }
}

I have changed "79 %" to a number 79 and all other numbers as well (because otherwise you will never get true on "79" >= "79 %"). Also I have changed conditions from "else if" to "if". because all numbers which are bigger than 79 will fall under that condition, and your code will not proceed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.