Is there a way to simplify this statement to be a 'prettier' comparison:
a <= 1 ||
b <= 1 ||
d <= 1 ||
!c;
You could put a, b, and d in an array, and check whether some of them are <= 1:
[a, b, d].some(num => num <= 1) || !c
Related
I have that expression
if (a === Infinity && b === 0 || a === -Infinity && b === 0 || a === 0 && b === Infinity || a === 0 && b === -Infinity) {
return NaN
}
I want short it, but I have no idea how to do this
UPDATE
If it possible, I cant use isFinite(), how to shorten else?
You can use !isFinite() to test if it's either Infinity or -Infinity.
if ((!isFinite(a) && b === 0) || (!isFinite(b) && a === 0)) {
return NaN;
}
If a and b are number typed, then:
if (!(a*b || isFinite(a+b))) return NaN;
If your linter warns about use of global functions, then:
if (!(a*b || Number.isFinite(a+b))) return NaN;
If you can't use multiplication:
if (!(a && b || Number.isFinite(a+b))) return NaN;
Math.absing both and then checking that the minimum is 0 and the maximum is infinity should do it.
const nums = [a, b].map(Math.abs);
if (Math.min(...nums) === 0 && Math.max(...nums) === Infinity) {
return NaN;
}
Can also sort the array.
const nums = [a, b]
.map(Math.abs)
.sort(a, b) => a - b); // .sort() works too, but could be more confusing
if (nums[0] === 0 && nums[1] === Infinity)) {
return NaN;
}
so here is the scenario
if ( ( a >= b) && ( (c < d) && (d != '') ) )
cannot get this logic on this to work correctly
so if d = '' it would cause that to be false. which would mean that the whole thing would equate to false. Problem is I need it to trigger when a >= b but also needs to include the and for c < d but only if d != '', in other words ignore the c < d part if d = '', otherwise used the c < d part to prevent a >= b from triggering.
hope this is making sense. I am trying to avoid doing and if/else or switch.
One comment would be if you are not absolutely avoid using if/else or switch then you could define funcitons use them and use thiss functions in your statement above.
Apart from that, changing the order of some expressions might do what you want. c < d would not be evaluated if d = '' is not true. a >= b would not be evaluated if the expresion before second or, ||, is true or d == '', empty string, is true.
if ((d != '' && c < d) || (d != '' && c >= d && a >= b))
which is;
if (d != '' && (c < d || a >= b))
thanks for your suggestions, I found a workaround though
I just put
if (d == '') { d = Infinity; }
then
if ( (a >= b) && (c < d) )
and that solved it
thanks for everyone's help
I have a function that returns a condition using four variables.
(payload, variables) => {
return payload.newMessage.lenderId === variables.lenderId && payload.newMessage.user.id === variables.authId
}
I want to be able to say C === D is only if C and D exists. What would be the optimal expression for this? So A === B is a sufficient condition if C and D doesn't exist, but if C and D exist, A === B and C === D both have to be met.
A and B must always be equal, and one of the following must be true:
There is no C
There is no D
C and D are equal
(A === B) && (!C || !D || C === D)
Beyond this construction, you should know what you mean by "exists" - is it enough that they not be undefined? Is it any truthy value? Etc.
function customCheck(a,b,c,d) {
const abComparison = a === b;
if (c && d) {
return abComparison && (c === d);
}
return abComparison;
}
From your description seems something like above?
I was wondering if i could write this
if ( (a > b) == (a > c) ) { ...
instead of this
if ( (a > b) && (a > c) ) || ( (a < b) && (a < c) ) { ...
a, b and c are numbers
You can write it, but they are not equivalent expressions (try having all 3 variables be the same number).
Yes, you can. In JavaScript, the comparison operators result in booleans, which can be directly compared with ==/=== and !=/!==, there's no conversion involved in those comparisons. (They can also be compared with > and < and such: true is greater than false. In that case, there is conversion involved, they get converted to numbers, true => 1 && false => 0.)
Your first code block will branch into the if block if a > b and a > c, or if a <= b and a <= c. (That's not quite what your second block does, mind, as Scott Hunter points out.) The correct comparison would be that this:
if ( (a > b) == (a > c) ) {
is equivalent to
if ( (a > b) && (a > c) ) || (a <= b) && (a <= c) ) {
Let's check:
function check(a, b, c) {
// Your first code block
const r1 = (a > b) == (a > c);
// Your second, which isn't _quite_ the same
const r2 = ( (a > b) && (a > c) ) || ( (a < b) && (a < c) );
// My second
const r3 = ( (a > b) && (a > c) ) || ( (a <= b) && (a <= c) );
console.log(a, b, c, "=>", r1, r2, r3);
}
check(1, 2, 3);
check(2, 1, 3);
check(3, 2, 1);
check(1, 1, 2);
check(2, 2, 1);
check(2, 1, 2);
check(1, 1, 1);
.as-console-wrapper {
max-height: 100% !important;
}
I am trying to sort a select option with value and text as follows .
The Text can have special characters and it needs to be sorted .
However I am finding that , some special characters are coming after alphabets .
I want all special characters first and then Alphabets .
c = [["#test","#test"], ["?test", "?test"], ["test", "test"], ["TEst", "TEst"], ["]test", "]test"]]
>>> c.sort()
[["#test", "#test"], ["?test", "?test"], ["TEst", "TEst"], ["]test", "]test"], ["test", "test"]]
The problem seems to be 'TEst' .
Another simple example:
cool = ['#new','?new','[new',']new','NEw','&new','cool','ind']
["#new", "?new", "[new", "]new", "NEw", "&new", "cool", "ind"]
cool.sort()
["#new", "&new", "?new", "NEw", "[new", "]new", "cool", "ind"]
The issue is specifically with characters with ASCII codes 91-96 and 123-126, which are punctuation or special characters yet have higher codes than the alphabetic characters. So your sort function needs to take that into account.
You could do this, for instance, by mapping those characters onto lower ASCII characters. http://jsfiddle.net/LGjnY/4/
function transformString(s) {
var r = '',
code;
for (var i = 0; i < s.length; i++) {
code = s.charCodeAt(i);
// map 91-96 onto 22-27
if (code >= 91 && code <= 96) code -= 69;
// map 123-126 onto 28-31
else if (code >= 123 && code <= 126) code -= 95;
r += String.fromCharCode(code);
}
return r;
}
c.sort(function (a, b) {
return transformString(a[0]).localeCompare(transformString(b[0]));
});
Or combining the comparison and transformation to make it faster (jsfiddle; not really tested)
function compareTransformedStrings(a, b) {
if (a == b) return 0;
for (var i = 0, A, B; (A = a.charCodeAt(i)) && (B = b.charCodeAt(i)); i++) {
if (A != B) {
return A - (A >= 91 && A <= 96) * 69 - (A >= 123 && A <= 126) * 95
< B - (B >= 91 && B <= 96) * 69 - (B >= 123 && B <= 126) * 95
? -1 : 1;
}
}
return a.length < b.length ? -1 : 1;
}
c.sort(function (a, b) {
return compareTransformedStrings(a[0], b[0]);
});
You can pass a comparison function as a .sort() function argument, like
c.sort(function(a, b) {
if (a.charCodeAt(0) <= 49) {
return -1;
}
return a.localeCompare(b);
})
Demo: http://jsfiddle.net/7DUEg/