Jump to content

Booleans Values Changing By Themselves !?


fuchs63

Recommended Posts

Dear All, year another oddity I came across with booleans. They don't like me as it seems. The Problem:Somehow, booleans seem to change their value by themselves. Hard to believe, I know... but read on and see yourself. I am testing a text string for the appearance of certain patterns. One of these patterns exists in the text, the other one doesn't. If the pattern is found, the value of a boolean changes from false to true. The initial value of the booleans is false. If the boolan is true, a confirming message about the existence of the pattern in the text is given out. In the Example belowI am testing the string "Jane is dark haired and has a dog " for the existence of the words 'dark' and 'cat'. When either of these two is found, a confirming message goes out. While 'dark' is there, 'cat' is not. Hence we should see only one confirming message. We do, however, see two messages. Somehow, the value of 'hasCat' changes from 'false' to 'true' by itself. Once more, I am off even the slightest idea how that can happen. The code was tested using IE 6 under WinXP Home. Again any help welcome!FuchsP. S. Here's the code<html><body><script type="text/javascript">var txt = "Jane is dark haired and has a dog ";var isDark = new Boolean(false);var hasCat = new Boolean(false);var cr = "<BR \>";//Debug Message before anything is done say("Values of Booleans after Initialization" + cr);say("isDark = " + isDark.valueOf() + cr);say("hasCat = " + hasCat.valueOf() + cr);say(cr);//Test text for occurrence of patterns and set booleans accordinglyvar ptnDrk = new RegExp("dark");var ptnCat = new RegExp("cat"); if (ptnDrk.test(txt)) { say(ptnDrk + " found in text " + "\"" + txt + "\"" + cr); say("value of isDark changing" + cr + " from: " + isDark.valueOf() + cr); isDark = true ; //set boolean say("to : " + isDark.valueOf() + cr);} // if ptnDrk in textelse say(ptnDrk + " NOT found in text " + "\"" + txt + "\"" + cr + "- no changes made to isDark" + cr);say(cr);if (ptnCat.test(txt)) { say(ptnCat + " found in text " + "\"" + txt + "\"" + cr); say("value of hasCat changing" + cr + "from: " + hasCat.valueOf() + cr); hasCat = true ; //set boolean say("to: " + hasCat.valueOf() + cr);} // if ptnCat in textelse say(ptnCat + " NOT found in text " + "\"" + txt + "\"" + cr + "- no changes made to hasCat" + cr);//Debug Message after changessay(cr + "Values of Booleans after Tests" + cr);say("isDark = " + isDark.valueOf() + cr);say("hasCat = " + hasCat.valueOf() + cr);say(cr + "Using Booleans in if-Statements" + cr);if(isDark) say("Jane is dark haired" + " *THIS IS EXPECTED TO APPEAR*" + cr);if(hasCat) say("Jane has a cat" + " *THIS SHOULD NEVER APPEAR, BUT STILL IT DOES... *" + cr);// -----------------------------------------------------function say(txt) { txt=txt.fixed(); document.write(txt);} // end function say()</script></body></html>

Link to comment
Share on other sites

According to http://www.devguru.com/technologies/ecmasc...ef/boolean.html, when the value of a Boolean object has been set to anything, even false, if you pass it to a conditional, it will evaluate to true. Fuchs, you can test this by passing hasCat.valueOf() to your say function or even just alert() in the final line of your script. I guess because it's an object with a value, a conditional just tests its existence, and yes it does exist. My impression is that a Boolean object is not really intended to be used this way.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...