0

function check()
{
    var rowl=document.getElementsByClassName("tbody")[0].rows;
    for(var r=0;r<rowl.length;r++)
    {
        ch=document.getElementsByClassName("tbody")[0].rows[r].cells[2].childNodes[0].value;
        
        var tiderow=document.getElementsByClassName("tide")[r];//rows[0].cells[1].childNodes[0].value;
        for(var t=0;t<tiderow.rows.length;t++)
        {
            var noo=tiderow.rows[t].cells[1].childNodes[0].value;
            console.log(noo);
            

        }
        
    }
}
Whenever I run this code, this prints correctly the subtable values. Before if condition And I am checking each subtable value with the Main table cells2 value. If it is greater, then I am showing the error message. But even though it is not greater it shows the error

function check()
{
    var rowl=document.getElementsByClassName("tbody")[0].rows;
    for(var r=0;r<rowl.length;r++)
    {
        ch=document.getElementsByClassName("tbody")[0].rows[r].cells[2].childNodes[0].value;
        
        var tiderow=document.getElementsByClassName("tide")[r];//rows[0].cells[1].childNodes[0].value;
        for(var t=0;t<tiderow.rows.length;t++)
        {
            var noo=tiderow.rows[t].cells[1].childNodes[0].value;
            console.log(noo);
            if(noo>ch)
            {
                console.log("Error occured");
            }

        }
        
    }
}

After inserting the if condition

2 Answers 2

2

Ah, but it is greater. You're comparing "5" to "10". "5", the string, is greater than "10", the string. If you want to compare these as numbers, convert them to numbers before the comparison:

ch = Number(rowl[r].cells[2].childNodes[0].value);
var noo = Number(tiderow.rows[t].cells[1].childNodes[0].value);
Sign up to request clarification or add additional context in comments.

3 Comments

What is this thing you're doing? Just use the unary operator +.
@AngelPolitis A matter of style. It always takes me just a little longer to figure out what's going on with the unary plus.
Surely, but since the only thing it does is convert something to a number, I believe it's just a matter of getting used to it. Great answer nonetheless! +1
1

you should do

parseInt(noo)>parseInt(ch)

else javascript will treat this variables as string so 5 is greater than 10

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.