Tauntaunwampa
Tauntaunwampa

Reputation: 61

Using the !== Operator to compare strings taken froma Dropdown list - Javascript

I'm trying to get q1check to read a value from the dropdown form item 'qq1'.' It's reading it in fine. There seems to be an issue with the way I'm using the comparison operators. Could anyone help me?

function q1ans_check(){
    var q1check = document.getElementById("qq1").value;
    alert(q1check);
    if(q1check !== "1" || qlcheck !=="0")
    {
        alert("Please answer Question 1");
    }   
}

Upvotes: 2

Views: 122

Answers (1)

Pointy
Pointy

Reputation: 413767

Your use of !== is fine. The problem is with your use of || instead of &&. There's also a typo — you misspelled "q1check" in the second comparison.

The test in the if statement:

if(q1check !== "1" || q1check !=="0")

(assuming you fix the typo like I did) will always be true. In English, that means, "If q1check is not equal to the string "1" or if q1check is not equal to the string "0", then ...". If the variable is neither "1" nor "0", then it's true. If it's "1", then the test is true because it's therefore not "0". If it is "0", then the test is true because it's not "1".

Upvotes: 2

Related Questions