admin管理员组文章数量:1406139
I need to design a global boolean with 2 conditions a and b such that if A is ever true, the boolean is true until b is true, at which point the boolean is false. In other words, if A bees false the boolean remains true.
I tried a straightforward global variable but it became false when A became false.
Preferably JavaScript, but pseudocode would be almost as helpful.
I need to design a global boolean with 2 conditions a and b such that if A is ever true, the boolean is true until b is true, at which point the boolean is false. In other words, if A bees false the boolean remains true.
I tried a straightforward global variable but it became false when A became false.
Preferably JavaScript, but pseudocode would be almost as helpful.
Share Improve this question edited Jun 5, 2012 at 19:47 Michael Myers♦ 192k47 gold badges298 silver badges295 bronze badges asked Jun 5, 2012 at 14:04 jamessonjamesson 3274 silver badges18 bronze badges 7- You need to put some logic in your setter and probably need some other variables too. – ChrisF ♦ Commented Jun 5, 2012 at 13:54
- Sorry I'm slow but I'm not seeing it. Any additional variables I make would be dependent on the first variable, which would be dependent on A and therefore false when A is false? – jamesson Commented Jun 5, 2012 at 13:58
- Sounds like the logic equivalent of a flip-flop circuit. – Diodeus - James MacFarlane Commented Jun 5, 2012 at 14:06
- Is there only one A and one B or does this need to be re-used? – Diodeus - James MacFarlane Commented Jun 5, 2012 at 14:07
- I believe flip-flop is correct. Yes, only one a and b – jamesson Commented Jun 5, 2012 at 14:16
5 Answers
Reset to default 2This sounds like an XOR. i.e.
!A and !B == false
A and !B == true
!A and B == true
A and B == false
Unfortunately, JavaScript doesn't have a logical XOR operator, however
if( A ? !B : B ) {
is functionally equivalent
If I understand your question correctly, it should be fairly easy to match those cases with
var bool = (a != b);
/*
(false != false) = false
(true != false) = true
(false != true) = true
(true != true) = false
*/
With your changes, you could create a global variable var aWasEverTrue = a;
and then instead of setting a
directly, use a function such as setA(true)
.
var a = false;
var b = false;
var aWasEverTrue = a;
function setA(newAValue) {
aWasEverTrue = true;
a = newAValue;
}
// (aWasEverTrue != b) = false
setA(true);
// (aWasEverTrue != b) = true
b = true;
// (aWasEverTrue != b) = false
setA(false);
// (aWasEverTrue != b) = false (as aWasEverTrue is still true)
b = false
// (aWasEverTrue != b) = true
What you want is a state machine
States for result:
T (True)
F (False)
Transitions:
F -- a (true) --> T
F -- anything else --> F
T -- b (true) --> F
T -- anything else --> T
You can express it with a series of if
s
Javascript old-school way:
function Enjoy() {
this.a = true;
this.b = true;
this.bool = true;
}
Enjoy.prototype = {
constructor: Enjoy,
setA: function( val ) {
this.a = val;
if ( this.a === true && this.b === true ) this.bool = false;
else if ( this.a === true && this.b === false ) this.bool = true;
},
setB: function( val ) {
this.b = val;
if ( this.a === true && this.b === true ) this.bool = true;
else if ( this.a === true && this.b === false ) this.bool = false;
},
getBool: function() {
return this.bool;
}
};
var enjoy = new Enjoy();
enjoy.getBool(); // true
enjoy.setB( false );
enjoy.getBool(); // false
As you can see, the idea is to use getters/setters for your boolean and both a
and b
variables where you do all your logic.
By the way, this question is definitely for StackOverflow.
Based on some assumptions about how B should behave if A has not been true:
function FlipFlop(){
this.latch = false;
this.value = false;
}
FlipFlop.prototype = {
constructor: FlipFlop,
setA: function( val ) {
this.latch = this.latch || !!val;
this.value = this.latch;
},
setB: function( val ) {
if(this.latch && !!val) {
this.latch = false;
}
this.value = !val;
},
getVal: function() {
return this.value;
}
}
本文标签: JavaScript set boolean by separate conditionsStack Overflow
版权声明:本文标题:JavaScript set boolean by separate conditions? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744979914a2635752.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论