-1

I have a bunch of variables having boolean values. I wish to have another variable that stores the name of the boolean variable changed last. So, next time when a new boolean variable value is changed, I want to toggle the previous boolean variable.

Any idea/suggestion to achieve the above would be highly appreciated.

Eg. it would be something like this incorrect code-

isDemo1=false; isDemo2=false; isDemo3=true; isDemo4=false; isDemo5=false; 

lastChangedBooleanVariable = this.isDemo3;

handleBooleanVaiables(currentChangedBooleanVariable: string)
{
// somehow toggle this.isDemo3 variable value
this.lastChangedBooleanVariable = currentChangedBooleanVariable;
// let's say currentChangedBooleanVariable = isDemo4
// somehow toggle this.isDemo4 value
}


Motomotes
  • 4,111
  • 1
  • 25
  • 24
Aditya
  • 37
  • 10
  • 1
    An [Array](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Arrays) and a variable for the index will help you here. – Knight Industries Oct 15 '22 at 08:56
  • Do you just want to keep track which of the Boolean variables were changed last? Is it possible for more than 1 to change at the same time? And is the number of Boolean variables always 5? – FaitAccompli Oct 15 '22 at 09:01
  • @KnightIndustries That was my first thought, but I don't want to manually enter multiple variables into an array. Also, these boolean variables are bound to HTML part of the component. – Aditya Oct 15 '22 at 09:02
  • @FaitAccompli No, only one variable at a time. No, total number can be more than 5. – Aditya Oct 15 '22 at 09:03
  • I believe the questioner also wants to know how to get the variable name and how to toggle the variable, not merely how to reference an object property by name. – Motomotes Oct 15 '22 at 09:19

4 Answers4

1

Everytime that you need to do some weird code, rethink your approach, it will probably wrong.

You can instead use Arrays:

isDemo = [false, false, true, false, false];

lastChangedBooleanVariable = 3;


handleBooleanVaiables(currentChangedBooleanVariable: int) {
  this.lastChangedBooleanVariable = currentChangedBooleanVariable;
  isDemo[currentChangedBooleanVariable - 1] = !isDemo[currentChangedBooleanVariable - 1]
}
Elias Soares
  • 9,884
  • 4
  • 29
  • 59
1

Sounds very much like what you're trying to do is functionally equivalent to:

let demo = 3;

Then set the value of this variable to any value from 1 to 5.
Instead of if (isDemo2), test if (demo == 2).
The "last changed variable" is always simply the current value demo holds, and that's probably obsolete information with this approach.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • Since you mentioned that it can be more than 5 Boolean variables bound to HTML components and one can be changed at a time then this is the best approach. – FaitAccompli Oct 15 '22 at 09:14
0

ES6 has a feature on Arrays called fill which can be pretty handy here. Essentially, you just set all the values to false before you activate a new version.

let demos = new Array(5).fill(false);

function activateDemo(demo) {
  demos = new Array(5).fill(false);
  demos[demo] = true;
}

activateDemo(4);

let demos = new Array(5).fill(false);

function activateDemo(demo) {
  const humanIndex = demo > 0 ? demo - 1 : 0; // to make it into human counting form, if you want.
  demos = new Array(5).fill(false);
  demos[humanIndex] = true;

}


activateDemo(4);
console.log("4", demos);
activateDemo(3);
console.log("3", demos);
Joel
  • 5,732
  • 4
  • 37
  • 65
0
var keys = {
"isDemo1":false, "isDemo2":false, "isDemo3":false, "isDemo4":false, "isDemo5":false;
};
var lastChangedBooleanVariable: string = "isDemo3";
const nameOf = (f) => (f).toString().replace(/[ |\(\)=>]/g,'');
function getName(varName:string){
    return varName.substring(
        varName.indexOf(".") + 1, 
        varName.lastIndexOf(";")
    );
}
function handleBooleanVaiables(currentChangedBooleanVariable: string)
{
    console.log(keys[lastChangedBooleanVariable]);
    keys[lastChangedBooleanVariable] = !keys[lastChangedBooleanVariable];
    keys[currentChangedBooleanVariable] = ! keys[currentChangedBooleanVariable];

    lastChangedBooleanVariable = currentChangedBooleanVariable;
    // let's say currentChangedBooleanVariable = isDemo4
    // somehow toggle this.isDemo4 value
}
function printValues(){
    console.log(keys);
}


var varName = getName(nameOf(()=>keys.isDemo1));
handleBooleanVaiables(varName);
printValues();

varName = getName(nameOf(()=>keys.isDemo4));
handleBooleanVaiables(varName);
printValues();

here's fiddle: https://jsfiddle.net/hjv540tk/

Nitin Sawant
  • 7,278
  • 9
  • 52
  • 98