How do I shorthand boolean language for writing conditions? - javascript

I have a list of variables that I call tags. Each one changes value from true to false depending on the function executed. These 'tags' act as builder pieces for booleans that I'd like to add in other variables. Basically, I'm trying to shorthand the language in order to make complex boolean conditions. Is this code correct for Javascript? The conditions are not passing as I've intended. cleared would read as true while runScreen and runIndicator read as true
Update I shortened the code and made a function to help strengthen the emphasis on the issue. Please see the code below.
Thanks
//builder variables for condition
let testValueA;
let testValueB;
//condition shorthanded inside a variable
const testCondition = !testValueA && testValueB;
// test function for condition
function testCondionValue() {
testValueA=Math.random() > 0.5;
testValueB=Math.random() > 0.5;
console.log("testValueA is", testValueA, "and testValueB is", testValueB);
if (testCondition) {
console.log(" therefore testCondition is true")
}
else {
console.log("therefore testCondition is false")
}
}

This line:
var cleared = (noHover && !runScreen && !runIndicator);
runScreen is true, then !runScreen is false, then cleared is false.

Related

React/Javascript Comparison Operator Not Working

Somehow I couldn't get my comparison operators working. I have the following piece of code:
function handleNotifClickRemind(key) {
localStorage.setItem('no_remind_change_pwd', true);
notification.close(key);
}
// ...
<Button outline size="small" onClick={() => handleNotifClickRemind(key)}>
Dont remind me
</Button>
// ...
console.log(localStorage.getItem('no_remind_change_pwd'));
function another_func(data) {
if (localStorage.getItem('no_remind_change_pwd') != true) {
openNotification('topRight');
}
}
When I clicked the "Don't remind me" button, handleNotifClickRemind was triggered because the log output on the third section prints true. However, openNotification was still being triggered. Can anyone please help?
P.S. I didn't initialize the value, I just let no_remind_change_pwd be null.
Thanks in advance.
All you've saved in localstorage are strings.
So you are comparing "true"!=true which is always true.
If you want to compare the value, you can use like the following.
JSON.parse(localStorage.getItem('no_remind_change_pwd')) != true
That means, the following is true
console.log(JSON.parse("true") === true)
It's always good practice to use triple comparison operator like === or !==.
localStorage.getItem() return data in String format so if you want to compare with some boolean then first convert it to boolean. By Boolean(localStorage.getItem('no_remind_change_pwd')).
function another_func(data) {
if (Boolean(localStorage.getItem('no_remind_change_pwd')) !== true) {
openNotification('topRight');
}
}
Hope this should clear your understanding.

TypeError: if 'false' not working as expected

I'm doing a PWA quiz application using React.js and I've met the following problematic:
I can get questions objects with only one answer, and some with multiple.
In the case there is only one possible answer, I want to force the user to only have one possibility.
To do that, I made the following algorithm:
clickOnChoice = (key) => {
if (this.state && this.state.correctAnswers) {
let newChoices = INITIAL_CHOICES; // {}
if (this.state.multiChoice) {
console.log("this.state.multiChoice:", this.state.multiChoice); // this.state.multiChoice: false ???
newChoices = JSON.parse(JSON.stringify(this.state.choices)); // {answer_b: 1}
}
newChoices[key] = 1 - (newChoices[key] | 0); // {answer_b: 1, answer_a: 1}
this.setState({
choices: newChoices
}, this.updateNextButtonState);
}
}
However the execution seems to ignore the condition if (this.state.multiChoice).
What am I missing?
Maybe I need a cup of coffee... ☕
Anyway, thanks in advance!
It is more than likely you are trying to checking a string of 'false' rather than an actual boolean value.
you can check that the string is the expected boolean if (this.state.multiChoice === 'true') or change the value of the state property to true || false

IF condition using string

I am programming in Polymer 1.0 and am trying to create an IF function to change the value of a property. My function is the following:
_searchButton: function(selectednamedropdown, selectedtypedropdown){
if (selectednamedropdown=="no_name_selected" && selectedtypedropdown=="no_type_selected"){
this.searchUsagesBtn = true
} else{
this.searchUsagesBtn = false
}
}
In my mind when selectednamedropdown is equal to "no_name_selected" and selectedtypedropdown is equal to "no_type_selected" the function should set searchUsagesBtn to true and when they are not these values, false.
However, the function does not ever seem to be returning true even when these conditions are met. Any ideas why this might be? Thanks for all help
When I run your function like this:
let searchUsagesBtn;
function search(selectednamedropdown, selectedtypedropdown) {
if (
selectednamedropdown === "no_name_selected" &&
selectedtypedropdown === "no_type_selected"
) {
searchUsagesBtn = true;
} else {
searchUsagesBtn = false;
}
}
search("no_name_selected", "no_type_selected");
console.log("button: ", searchUsagesBtn);
I get button: true in console log. So maybe your inputs in this function are not a strings.
The issue was around how JavaScript treats properties within functions. The function was storing the new value and old value of the first property and not any values of the second property. The solution involved making 2 functions to test the strings in each property. Thanks for all assistance

Creating a function that loops through an array and flags index's that are do not match a criteria

I'm trying to create a function that returns false if the hours of this business are different. So for instance if the business hours today are a little bit different than tomorrow, then it should return false. I'm working with some endpoints that provide me with the data I need for the next seven days and am trying to use a while loop to loop through them and match each against each other. I keep running into an error while I try writing this loop in the console stating that there is an end of input error.
while (hours[i].start) {
if (hours[i].start) == (hours[i+1].start)
test_object = false
end
}
The error i'm getting is an unexpected token ==
I've always been weak with JS looping, would anybody be able to help me through this situation?
You are missing a bracket
if ((hours[i].start) == (hours[i+1].start))
Also remove the end. The while loop would stop once the condition doesn't satisfy. You can read more about it here
while (hours[i].start) {
if ((hours[i].start) == (hours[i+1].start))
test_object = false
}
You could use Array#every and check every pairs.
var test_object = hours.every((h, i, hh) => !i || h.start !== hh[i - 1].start);
Edit
while (hours[i].start) {
if (hours[i].start) == (hours[i+1].start)
test_object = false
end
}
to
while (hours[i].start) {
if ((hours[i].start) === (hours[i+1].start))
test_object = false
}
Also remove the end statement. There is no end keyword in javascript

can arangoDB aql skip filters by condition?

i work for a bbs app recently, and when i use the aql, i got a problem, for example:
//params:.com/t/123?&digest=true
=>
FOR t IN threads
FILTER t.digest == true && some conditions
RETURN t
and the result is as expected,but when the parameter 'digest' is undefined or false, i want it returns all the threads..not which 'digest' = false, so i have to use if...else to write two almost same code, just remove the condition 't.digest == true' for example:
if(params.digest) {
return `digest == true && conditions`
}
return `conditions`
i'm using javascript, please give me a direction...
You can use the function HAS(document, attributeName) to check whether the attribute is present in the document.
FOR t IN threads
FILTER (!HAS(t,"digest") || i.digest == true) && conditions
RETURN t

Categories

Resources