I have a process that runs when the command is issued in the discord bot, sometimes this process will fail and I need to know when it does. Every time after the command is issued and the process finishes it logs in console
console.log ('Process done! All succeded: %s || All failed: %s'), allsucces, allfailed
So every time all succeded = 0 I want the bot to dm me in a discord message
You can simply compare allsucces value to 0
if (allsucces === 0) {
/* DM Logic */
}
These links might also provide you with some useful information regarding Javascript and how Comparison and logical Operators work:
Javascript Basics
JavaScript Comparison and Logical Operators
To compare 2 values, use comparison operators.
Here's how they work:
val == 0 // val has the value 0 but does not *need* to be the same type ("0" == 0 is true)
val === 0 // val is 0, both in value and type
val != 0 // opposite of "==" - val does not have the value 0
val !== 0 // opposite of "===" - val does not have the value 0, or is not the same type
val > 0 // val is more than 0
val < 0 // val is less than 0
val >= 0 // val is more than or equal to 0
val <= 0 // val is less than or equal to 0
See the difference between === and == in this question
To implement this in your code, use the === operator
if (allsucces === 0) {
// allsucces is equal to 0
}
And be sure allsucces is a number, not a string, otherwise the strict equals operator will make it return false!
Related
This question already has answers here:
Why `null >= 0 && null <= 0` but not `null == 0`?
(6 answers)
Closed 8 months ago.
can someone explain the null comparison with 0
null > 0 // false
null == 0 // false
null > 0 || null == 0 // false
null >= 0 // true
why it happening ?
The reason is that an equality check == and comparisons > < >= <= work differently. Comparisons convert null to a number, treating it as 0. That’s why null >= 0 is true and null > 0 is false.
On the other hand, the equality check == for undefined and null is defined such that, without any conversions, they equal each other and don’t equal anything else. That’s why null == 0 is false.
Source: Comparisons
In Javascript null isn't equal to 0 but equal to undefined.
null == undefined // true
Also :
Strange result: null vs 0
Let’s compare null with a zero:
alert( null > 0 ); // (1) false
alert( null == 0 ); // (2) false
alert( null >= 0 ); // (3) true
Mathematically, that’s strange. The
last result states that "null is greater than or equal to zero", so in
one of the comparisons above it must be true, but they are both false.
The reason is that an equality check == and comparisons > < >= <= work
differently. Comparisons convert null to a number, treating it as 0.
That’s why (3) null >= 0 is true and (1) null > 0 is false.
On the other hand, the equality check == for undefined and null is
defined such that, without any conversions, they equal each other and
don’t equal anything else. That’s why (2) null == 0 is false.
Comparisons
I'm creating a typing test in Javascript. When the user presses the keyboard button, the typing test begins if zero keys have been pressed previously. If textEnteredLength (keys pressed) is === 0 and !timerRunning is equal to not false. Why does this function run? 0 === not false, is not a true statement, right?
var timerRunning = false;
function start() {
let textEnterdLength = testArea.value.length;
if (textEnterdLength === 0 && !timerRunning){
timerRunning = true;
interval = setInterval(runTimer, 10);
}
Due to operator precedence, this expression:
textEnterdLength === 0 && !timerRunning
is equivalent to
(textEnterdLength === 0) && !timerRunning
The 0 isn't compared against !timerRunning, but against textEnterdLength.
So, in the end it's
if (someBoolean && someOtherBoolean)
and if both are true, it'll run.
I need to validate the user input where user can only enter the value not equal to but greater than 0 and smaller than stated value. My validation logic works if i provide certain object
{required:true, greaterThan: 1, smallerThan: 50}
Here, if i input the value 0, then it displays an error message saying, value should be greater than 1 but if in case, i provide the following object instead
{required:true, greaterThan: 0, smallerThan: 50}
and I input the value 0 then it does not display an error message saying value should be greater than 0
Here is my validation logic
if (fieldValue.smallerThan && value && parseInt(value, 10) > fieldValue.smallerThan) {
set(
errors,
key,
`${fieldValue.label} should be smaller than ${fieldValue.smallerThan}`,
)
}
if (fieldValue.greaterThan && value && parseInt(value, 10) < fieldValue.greaterThan) {
set(
errors,
key,
`${fieldValue.label} should be greator than ${fieldValue.greaterThan}`,
)
}
0 && any value
Will always result in false because 0 is considered as falsy value in javaScript
What You can do is
fieldValue.greaterThan >= 0 && ..
If you're just checking for existence of the greater than property in the fieldValue, which seems to be what you're doing, you might also considering editing your two checks to be:
if (fieldValue.smallerThan !== undefined && ...)
and
if (fieldValue.greaterThan !== undefined && ...)
In general, relying on the truthiness or falsiness of values gets you into trouble, and it's better to be explicit about what you are checking for.
always convert your inputs to an integer or float.
if(!(fieldValue.smallerThan < parseInt(value))){
...value must be smaller than 50
}
if(!(fieldValue.greaterThan > parseInt(value))){
...value must be greater than 0
}
I`m trying to perform some action with document.cookie,
I checking some If conditional mentioned below:
if((!document.cookie.indexOf('cookies:all_checked') >= 0) ||
(!document.cookie.indexOf('cookies:first_and_second') != -1) ||
(!document.cookie.indexOf('cookies:first_and_third') != -1) ||
(!document.cookie.indexOf('cookies:first_only') != -1)){
createCookies();
}
before this statement I create cookie like this
var variable = document.cookie == 'cookies:all_checked'
the trouble is no matter I`m trying do check
(!document.cookie.indexOf('cookies:all_checked') >= 0)
or the same without ! mark I get true with this condition. How to check is it cookies with this content is really setted?
Your problem is that
document.cookie.indexOf('cookies:all_checked')
will return either the index, or -1 (when not found).
In your example, it will return 0 because document.cookie starts with the string you're looking for.
Then, !0 will evaluate to true as 0 evaluates to false, and true evaluates to 1, which is indeed >= 0.
And 0 is also >= 0.
That's why you get true everytime.
You should try:
!(document.cookie.indexOf('...') >= 0) // false
document.cookie.indexOf('...') >= 0 // true
Note that !anyNumber will result in either true or false which will both be >= 0 as true will evaluate to 1 and false to 0 which are both >= 0
Therefore, !anyNumber >= 0 will always be true.
As a side note, please be aware that:
var variable = document.cookie == 'cookies:all_checked'
does not set/create your cookie.
What it does is check whether cookie equals 'cookies:all_checked' and assigns this result (true or false) to variable.
I have a var a;
Its value can be NaN, null and any +ve/-ve number including 0.
I require a condition which filters out all the values of a such that only >=0 values yield a true in if condition.
What is the best possible way to achieve this, I do not wish to use 3 different conditions joined using ||
typeof x == "number" && x >= 0
This works as follows:
null -- typeof null == "object" so first part of expression returns false
NaN -- typeof NaN == "number" but NaN is not greater than, less than or equal to any number including itself so second part of expression returns false
number -- any other number greater than or equal to zero the expression returns true
Ohk ...But i actually found the ans ..
it is so Simple .
parseInt(null) = NaN.
So if(parseInt(a)>=0){} would do ...Yayyee
NaN is not >= 0, so the only exclusion you need to make is for null:
if (a !== null && a >= 0) {
...
}
This seems to work well:
if (parseFloat(x) === Math.sqrt(x*x))...
Test:
isPositive = function(x) { return parseFloat(x) === Math.sqrt(x*x) }
a = [null, +"xx", -100, 0, 100]
a.forEach(function(x) { console.log(x, isPositive(x))})
My best solution to filter those values out would be with 2 condition and it is like;
if(a!=undefined && a>=0){
console.log('My variable is filtered out.')
}
I am not sure but there is no single condition usage to make it.
I had the same problem some weeks ago, I solved it with:
if(~~Number(test1)>0) {
//...
}
http://jsfiddle.net/pT7pp/2/
Since you tagged jQuery, take a look at $.isNumeric()
if($.isNumeric(a) && a >= 0)