I am looking at a more elegant way to match and either or in JavaScript.
With strings, we can do the following:
"test".match(/test|otherstring/)
But the number object has no method match. We can of course add this, but my application will be dropped into a large international site with hundreds of separate applications, so this would be a dangerous move.
At this point I have two options:
String(myNumber).match(/test|numbers/)
Or:
if(number === test1 || number === test2)
But neither of these solutions seem very elegant to me. What I would like is to be able to do:
number === test1||test2;
But the result of this is either true if number===test1 of if not it returns test2. Or use brackets:
number === (test1||test2)
But this only compares the first test and returns the result of that.
Here we can have a little more fun with JavaScript wierdness. Bonus points for anyone who can tell me why the following sequence occurs:
13===(13|13)
# true
13===(12|13)
# true
13===(1|13)
# true
13===(133|13)
# false
13===(13|133)
# false
for this question
13===(13|13)
# true
13===(12|13)
# true
13===(1|13)
# true
13===(133|13)
# false
13===(13|133)
# false
A single pipe is a bit-wise OR.
example
alert(6 | 10); // should show 14
8 4 2 1
-------
0 1 1 0 = 6 (4 + 2)
1 0 1 0 = 10 (8 + 2)
=======
1 1 1 0 = 14 (8 + 4 + 2)
Related
Just like the title says
Description of the example:
x) example string -> what I need as the output
examples:
1) "asdf" -> null or -1 or undefined (because nothing was found)
2) "asdf1" -> 1
3) "asdf0" -> 0
4) "asdf-1" -> 1
5) "asdf001" -> 1
6) "asdf1234" -> 1234
7) "asdf12.34" -> 34 (ending value, so number after .)
8) "123asdf78" -> 78 (integer from ending)
I hope these examples will be enough. I tried doing this with for loop, but it didn't work out. Does anyone know if there are functions which enable to do something like this or similar?
More info about my approach:
In the for loop I checked for every character if is >= '0' && <= '9' then added to previous tmp variable which responsible for concatenate all characters, and at the end I parsed this to int. But I think this solution is bad...
You can use simple regular expression for this purpose:
function numericSuffix(string) {
const match = string.match(/\d+$/);
return match !== null ? Number(match[0]) : null;
}
numericSuffix('abc-123') // 123
numericSuffix('abc-123x') // null
My .NET backend handle enum authorization with HasFlag()
Enum Foo {
0: Zero,
1: One,
2: Two,
3: Three,
..
}
5.HasEnum(Foo.One) is falsy because 5 is equal to 3 + 2
6.HasEnum(Foo.One) is truthy because 6 is equal to 3 + 2 + 1
How to handle it in JavaScript ?
I'm receiving a rôle (that is > 0 and < 1048), how to know if it has 32 rôle ?
This question already has answers here:
find the only unpaired element in the array
(10 answers)
What are bitwise operators?
(9 answers)
What do these JavaScript bitwise operators do?
(3 answers)
Closed 5 years ago.
Saw an interesting piece of code to find a lonely number in a list of duplicate numbers (where every number in the list occurs twice except for one).
function findNonPaired(listOfNumbers) {
let nonPairedNumber = 0
listOfNumbers.forEach((n) => {
nonPairedNumber ^= n
})
return nonPairedNumber
}
const x = [1,5,4,3,9,2,3,1,4,5,9]
console.log(findNonPaired(x))
This solution looks very elegant, but I'm curious at to what the ^= operator is actually doing here?
a ^= b is the same as a = a ^ b where ^ is the bitwise XOR operator.
0 ^ 0 === 0
1 ^ 0 === 1
0 ^ 1 === 1
1 ^ 1 === 0
This is a neat algorithm. Let's trace one execution with the list [8, 12, 8] for example:
0 ^ 8 = 0000 ^ 1000 = 1000
1000 ^ 1100 = 0100
0100 ^ 1000 = 1100 = 12
The word "duplicate" isn't correct. This algorithm tests for parity. A simple but somewhat wrong definition is "when everything has a pair". pair...parity.
[2,2,2,3,3,5,5,5,5] will return 2 because everything else has a pair.
[3,4,5] will actually return 2 (011^100^101 -> 010) because that is the xor of all the unpaired items.
Like other answers say - it is a bitwise XOR.
About the algorythm - it is cool if you are sure that the duplicates are even count. When a number is XOR-ed with x and later again XOR-ed with x it will return to it's previuos value. If one number is seen 3 times in this chain, the 3-rd occurence will fool this algorythm.
Also if there is one more value that is single in the chain, like:
a, b, c, X, a, c, b, Y
the result will be (X ^ Y) and you can't be sure if you have one unique value or more.
I'm trying to get a regex for an amount:
ANY DIGIT + PERIOD (at least zero, no more than one) + ANY DIGIT (at least zero no more than two [if possible, either zero OR two])
What I have is:
/^\d+\.\{0,1}+\d{0,2)+$/
...obviously not working. Examples of what I'm trying to do:
123 valid
123.00 valid
12.34.5 invalid
123.000 invalid
Trying to match an amount with or without the period. If the period is included, can only be once and no more than two digits after.
Make the decimal point and 1 or 2 digits after the decimal point into its own optional group:
/^\d+(\.\d{1,2})?$/
Tests:
> var re = /^\d+(\.\d{1,2})?$/
undefined
> re.test('123')
true
> re.test('123.00')
true
> re.test('123.')
false
> re.test('12.34.5')
false
> re.test('123.000')
false
Have you tried:
/^\d+(\.\d{1,2})?$/
The ? makes the group (\.\d{1, 2}) optional (i.e., matches 0 or 1 times).
Would something like this work?
// Check if string is currency
var isCurrency_re = /^\s*(\+|-)?((\d+(\.\d\d)?)|(\.\d\d))\s*$/;
function isCurrency (s) {
return String(s).search (isCurrency_re) != -1
}
This question already has answers here:
Why does (0 < 5 < 3) return true?
(14 answers)
Closed 1 year ago.
In one JS library I saw such syntax:
if (val > 5 == t) { ... }
I tested this in console:
1 == 1 == 2 // false
2 > 1 == 1 // true
1 == 2 == 1 // false
1 == 1 == 1 // true
1 < 2 < 3 // true
1 > 2 > 3 // false
At first glance all correct. Can this be used?
1 == 1 == 2 // this
true == 2 // becomes this
1 == 2 // which becomes this, and is false
2 > 1 == 1 // this
true == 1 // becomes this
1 == 1 // which becomes this, and is true
...and so on.
If you're wondering about the conversion, you should do a search on the == operator, which uses the Abstract Equality Comparison Algorithm.
You not really comparing what you think you are comparing:
(1 == 1) == 2 // actually true == 2 which is false
(1 == 2) == 1 // actually false == 1 which is false
Which is why strict equality === will fail in all cases
Javascript does not support the chained comparison syntax used in mathematics:
1 < 2 < 3 // 1 is less than 2 which is less than 3.
Instead, it evaluates each comparison left to right, which can sometimes yield the same results as mathematical chained comparison, as do all your examples, but the process is different:
1 < 2 < 3 // "1 is less than 2" is true, true is 1, "1 is less than 3" is true.
// Javascript returns true.
3 < 2 < 1 // "3 is less than 2" is false, false is 0, "0 is less than 1" is true.
// Javascript returns true.
For this reason, it should be discouraged.
To answer your question, however, yes it can be used.
It's a correct syntax but not one I would recommend. What's happening, is probably:
if ((val > 5) == t) { ... }
I tested this in console:
(1 == 1) == 2 // false
(2 > 1) == 1 // true
(1 == 2) == 1 // false
(1 == 1) == 1 // true
(1 < 2) < 3 // true
(1 > 2) > 3 // false
With the boolean on the left implicitly converted to an int.
There is nothing to stop you from using such an adventurous syntax however do keep in mind that one of the most frequent reasons for having bugs inside JavaScript code is messing up operator precedence.
Therefore its strongly advised to explicitly define precedence by adding brackets to precedence groups in case they consist of more than simple mathematical expressions for which the precedence is clearly determinable.
Another concern is type coercion.
jslint output:
Error:
Problem at line 2 character 13: Expected '===' and instead saw
'=='.
if (val > 5 == t) { }