frankp Posted December 2, 2009 Share Posted December 2, 2009 helloI have some problems with an if-statement not working. In this, I discovered that the XML is the problem, because I tried without calling the xmlsheet.However, the alert clearly displays the correct number(so there's no problem with accessing the info in the XML), which may be 0 or may be 3, but in both cases we always end up in the else-place.why is 0 not 0? x=xmlDoc.getElementsByTagName(xname); xcount=x[0].getElementsByTagName(xname.slice(0,1)+"count")[0].childNodes[0].nodeValue; alert(xcount.valueOf()); //this always displays the right number if(xcount=0){//this is the place I want to be if the xcount is 0 } else {//this is the else-place where I end up anyway } Link to comment Share on other sites More sharing options...
thescientist Posted December 2, 2009 Share Posted December 2, 2009 would it need to be if(xcount == 0)? Link to comment Share on other sites More sharing options...
frankp Posted December 2, 2009 Author Share Posted December 2, 2009 would it need to be if(xcount == 0)?well, it worked. Thank you. I've never seen this necessary in JavaScript...if I hadn't used the xml it would have worked anyway, so I've kinda though it was not a part of the language. In any case, thank you very much! Link to comment Share on other sites More sharing options...
thescientist Posted December 2, 2009 Share Posted December 2, 2009 I think the difference is in the use of = vs. ==. Where as = will just set xcount to 0, == will check the value of xcount to that number (0), which in turn seems to fill the conditional nature of if/else statements. With =, I think the code would just interpret it as you just setting the variable, where you want it to base its execution of the next lines of code on the condition of whether or not the if line meets a given condition. In other words, xcount = 0 fulfills itself, thus not setting a condition to be checked for logical operation(s).I think... Link to comment Share on other sites More sharing options...
thescientist Posted December 2, 2009 Share Posted December 2, 2009 maybe the XML issue with '=' was a type definition problem? (string vs. number/int) Link to comment Share on other sites More sharing options...
jeffman Posted December 2, 2009 Share Posted December 2, 2009 x=0 evaluates to the value of the assignment, which is 0. So if(x=0) is the same as evaluating the truth value of false. You end up in the else clause every time. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.