true == false evaluates to true somehow? - javascript

I've been working to scrape some webpage that is using the OWASP CRSFGuard project for protection. The library seems to be causing one of my requests to get a 401 so I started digging through their code and noticed the following;
function isValidDomain(current, target) {
var result = false;
/** check exact or subdomain match **/
if(current == target || current == 'localhost') {
result = true;
} else if(true == false) {
if(target.charAt(0) == '.') {
result = current.endsWith(target);
} else {
result = current.endsWith('.' + target);
}
}
return result;
}
From what I can tell, there must be instances where this code is executed; result = current.endsWith('.' + target);. Given true == false is inherently false, how would the code reach that statement? Is this some JS oddity (I know we're not using the strict === equality, but seriously...)?

Answer: It will never reach that code block.
function isValidDomain(current, target) {
var result = false;
/** check exact or subdomain match **/
if (current == target || current == 'localhost') {
result = true;
} else if (true == false) {
if (target.charAt(0) == '.') {
result = current.endsWith(target);
} else {
result = current.endsWith('.' + target);
}
}
return result;
}
var trueFalse = document.getElementById('trueFalse');
trueFalse.innerHTML = isValidDomain('true', 'false') ? 'WTF!' : 'All is good in the JS World';
trueFalse.addEventListener('click', function(e) {
trueFalse.innerHTML = (true == false) ? 'WTF!' : 'All is good in the JS World Still';
});
<div id="trueFalse"></div>

I would say that Blazemonger is most likely correct.
That else if probably had some other condition at some point, and for whatever reason, they decided they didn't want that block of code to execute anymore, so they changed the condition to something that is always false.
It's also not entirely uncommon to see programmers use 1 === 0 as an indication for false. Why they would want to do this is anybody's guess.

Related

How can I make it so that at any point during my function the user can type "reset" and it will take them back to the beginning of my function?

let level = 0;
let usersName;
let path;
const getBotReply = (msg) => {
if (level === 0) {
level = 1;
usersName = msg;
return "Chur " + usersName + ". Do you live in Raglan?";
}
if (level === 1) {
level = 2;
if (msg === "yes") {
path = "left-yes";
return "Do you know how to surf?";
}
if (msg === "no") {
path = "right-no";
return "Are you from Auckland?";
}
}
Basically I want it so that instead of typing yes or no. The user will type reset and it will return to "level 0"
I've tried:
if (msg === "reset") {
level = 0;
}
If you put your if-check for reset at the top then you have done it correctly, and you simply forgot a return in the solution you suggested?
If you don't include a return stament in the if-check the function will just continue, so it will run the next if check, which matches the level being 0 and it will assume that "reset" is the name of the user.
So if you include
if(msg === "reset"{
level = 0
return
}
It should work just fine, you can leave the return blank or include a message. I included a JS fiddle which is literally your code with the fixed if check: https://jsfiddle.net/5tu7c20n/
in this case, I believe it would be preferable to include a while loop.
while(msg === "reset"){level = 0};

checking for null in javascript

if (valid === null) {
return '';
} else if (!valid) {
return 'is-not-valid';
} else if (valid) {
return 'is-valid';
} else {
return '';
}
I have the above if-else-if chain in the code, trying to see if I can write the same logic in one or two lines.
Since you want to distinguish between three types of values, you have to make at least two checks already. The else case will never be hit since either !valid or valid will be true. That also means you can reduce the last else if to an else:
if (valid === null) {
return '';
} else if (!valid) {
return 'is-not-valid';
} else {
return 'is-valid';
}
But you could condense this logic using the conditional operator:
return valid === null ? '' : (valid ? 'is-valid' : 'is-not-valid');
Although I think your example (sans the redundant else block) looks pretty good, you can write it on one line like this:
return valid === null ? '' : (valid ? 'is-valid' : 'is-not-valid')
I prefer the original though

Page reading 2 different If-Statements as the same

I'm running into a pretty confusing situation here:
if ((a.length == 0 || b === null)) {
this.noNotif = true;
console.log("1");
}
if (a.length > 0 || b === null) {
this.newNotif = true;
// this.noNotif = false;
console.log("2");
} else {
if (a.length === b.length) {
console.log("No New Notifications");
this.noNotif = true;
} else {
console.log("New notifications");
this.newNotif = true;
}
Console logging a.length returns 0 and 'b' is null
However, the issue is that somehow both of the first two if-statements' conditions are being met. noNotif and newNotif both display a Vue components and both are showing up currently.
Some background information about a & b
'a' is supposed to be data from an API that is fetched on page load. 'b' is supposed to be a localStorage object array
The first if-statement deals with a new user who has no API data or anything stored in LocalStorage
The second if-statement handles when the user does have data in the API, but nothing in LS yet.
The First nested if-statement is if the data from the API matches the LS data
The nested else-statement is if the API data is different (longer) than what's in LS
EDITED:
It turns out you also didn't put an "else" statement between the two. They're both triggering because they're both registering the || b = null.
if ((a.length == 0 || b === null)) {
this.noNotif = true;
console.log("1");
} else if (a.length > 0 || b === null) {
this.newNotif = true;
// this.noNotif = false;
console.log("2");
} ...

Having problems with Javascript Else If statement in Node Red

I have been fighting this problem all day. Heres a snippet of my code, 5 lines down in the else if is where things get screwy. I am also new to Javascript so that may be the reason I am unable to spot the mistake, but from what I have seen elsewhere, this code should work. Also the comments on lines 5 and 6 are swapped.
if (msg.payload.License_Plate !== null) {
// This portion checks for a valid number plate
if (readlpr == dblpr); { // we have a direct match, open the gate
opengate = 1; // will send open signal to gpio
} else if(readlpr !== null); { // from here on, we are checking for a partial plate match
validdigits = 0; // make sure we have data before continuing, as may be a rfid match
{
if (!context.count); { // check to see if counter already used, if not initialise it
context.count = 0;
Image of error message
You have a few errors:
if (readlpr == dblpr); {
...
} else if(readlpr !== null); {
...
if (!context.count); {
And also an extra opening-brace.
These shouldn't have a semi colon on the end:
if (readlpr == dblpr) {
...
} else if(readlpr !== null) {
...
if (!context.count) {
In the end it should look something like this:
if (msg.payload.License_Plate !== null) {
if (readlpr == dblpr) {
opengate = 1;
} else if(readlpr !== null) {
validdigits = 0;
// { <!-- Remove this as well, it's an extra brace
if (!context.count) {
context.count = 0;

is this the right way to pull booleans from localstorage?

function loadChoice(key, varToStore) {
if (window.localStorage.getItem(key) === "true") {
varToStore = true;
}
else if (window.localStorage.getItem(key) === "false") {
varToStore = false;
}
else {
varToStore = window.localStorage.getItem(key);
}
}
I think I found a way to pull booleans (that got converted to strings before) from the web-browser localstorage.
can anyone confirm or have a better method ?

Categories

Resources