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
 |  Show 2 more ments

5 Answers 5

Reset to default 2

This 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 ifs

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