javascript Binary operation don't work for options - javascript

Here is what i have...
some array item have option that is binary
like
$item[0] = 0010
$item[1] = 1000
$item[2] = 0110
$item[3] = 1101
each bit represent some option
what i need is to compare it to the customer request option
let's say it's 0010
so i need the logic to show only $item[0] and $item[2] because second byte is 1. BUT, when there is no customer options check : 0000 i must show them all
only when some options are show there must be filter...
i should have listen more in my math class... i am clueless now, please help !
note :
everything come from here : http://jsfiddle.net/yHxue/
but i dont understand this line :
markers[m].setMap(((markers[m].props & props)>>>0===props)? ((props)?map:null): null);, so i have rewritten it, mine don't work !

If your data is actual numbers, then just use the bitwise & operator on each member.
var customerOption = parseInt("0010", 2)
$item.forEach(function(n, i) {
if (customerOption === 0 || ((n & customerOption) == customerOption))
alert(i); // show this item
});
If they're strings, then you'll need to convert each item to a number first.
Same as above, but...
parseInt(n, 2);
var $item = [];
$item[0] = "0010"
$item[1] = "1000"
$item[2] = "0110"
$item[3] = "1101"
var customerOption = parseInt("0010", 2)
$item.forEach(function(n, i) {
if (customerOption === 0 || ((parseInt(n, 2) & customerOption) == customerOption))
document.querySelector("pre").textContent += "\n0010 matched index: " + i;
});
document.querySelector("pre").textContent += "\n\n"
var customerOption = parseInt("0110", 2)
$item.forEach(function(n, i) {
if (customerOption === 0 || ((parseInt(n, 2) & customerOption) == customerOption))
document.querySelector("pre").textContent += "\n0110 matched index: " + i;
});
<pre></pre>

Convert all the strings (items as well as the mask) to a number using parseInt(string, 2).
If mask is 0, just take all the items. If not:
Then use the & operator to see the common 1 bits:
var common = item & mask;
Then test common:
If you want any of the mask bits, then common should just not be 0.
If you want all of the mask bits, then common should equal mask.
EDIT:
markers[m].setMap(
((markers[m].props & props) >>> 0 === props) ?
((props) ? map : null) :
null
);
markers[m].props & props looks for common 1 bits, as I explain above; >>> 0 makes sure the number is positive. The result is tested against props, making sure the common bits are all the bits in props (as in my second option above). If all the props bits are set, and if props is not zero, then markers[m] is setMap-ped to map; otherwise, to null.

var selections = ['0010', '1000', '0110', '1111'];
var getRequiredSelections = function (customerSelection) {
var customerSelectionInt = parseInt(customerSelection, 2);
if (customerSelectionInt) {
return selections.filter(function (binaryString) {
return parseInt(binaryString, 2) & customerSelectionInt;
});
} else {
return selections;
}
};
You can use it like so:
getRequiredSelections('0010');

BUT, when there is no customer options check : 0000 i must show them all
In 2's compliment, -1 is all 1 in binary
if (!props) props = -1;
Testing for something in binary exactly, use both & and ===
needle === (needle & haystack)
This means looking for needle = 1001 in haystack = 0001 is false, even though 1001 & 0001 is the truthy 0001
You may not want to use exact matches in your case
You could write some code that takes callbacks to process your array
function applyByFlag(flag, callbackTrue, callbackFalse) {
if (!flag)
flag = -1;
function nop() {}
if (!callbackTrue) callbackTrue = nop;
if (!callbackFalse) callbackFalse = nop;
boxes.forEach(function (e, i, a) {
if (flag & e) // not exact test
callbackTrue.call(e, e, i, a);
else
callbackFalse.call(e, e, i, a);
});
}
Of course, you'd probably want to adjust the test to work against e.props

The code you have is almost there; just add an additional check for when props is zero:
for(var m=0; m < markers.length; m++) {
var show = (markers[m].props & props)>>>0===props || props == 0;
markers[m].setMap(show ? map : null);
}
Demo
The markers[m].props & props expression will either be 0 (if not all bits of props match) or the value of props itself (if all bits of props match).
The additional >>>0 will turn the expression into an unsigned 32-bit integer value, but it's not necessary if you use the following expression:
var show = (markers[m].props & props) || props == 0;

Related

javascript string comparison not returning true for equal strings in console [solved]

I'm working with words and their phonemes. I found that in my code (and in the console) what looks like two identical strings " 'b eh1 r z'" for example are not returning true when compared, whether with double or triple equals. I did sanity tests in the chrome console, in the node console and in the file itself, and they all return expected results (i.e. only the 'strinfigied' variable seems corrupted. I'm racking my brains trying to figure what's going on. This is what is not workign as expected:
let stringified = trialPhonemeSequence.join(" ")
if (p == "z"){
console.log(trialPhonemeSequence)
let bearstring = 'b eh1 r z'
console.log("SHould be adding 'z' at ", i, "so we got", trialPhonemeSequence, "and stringified", stringified)
console.log(`String|${dictionary['bears']}| length ${dictionary['bears'].length} should equal |${stringified}| length ${stringified.length}: ${dictionary['bears'] == stringified} and ${bearstring == stringified}`);
}
What the Chrome Console outputs
String|b eh1 r z| length 10 should equal |b eh1 r z| length 10: false and false
Here is the entire function up to that point for context. I don't think you want the entire min reproduable code as it requires large dictionaries and datasets and initialization. The goal of this function was to input bear and look for words that are a phonemic match, allowing for addition of a phoneme (the 'z' sound in this test case).
function findAddedPhonemes(word, dictionary, hashMap)
{
let matches = []
let phonemeSequence = dictionary[word]
let phonemeSequenceList = phonemeSequence.split(" ")
for (let i = 0; i <= phonemeSequenceList.length; i++)
{
phonemeList.forEach((p, ind) => // all the items in the list
{
let trialPhonemeSequence = phonemeSequenceList.slice()
trialPhonemeSequence.splice(i, 0, p) // insert p phoneme into index
let stringified = trialPhonemeSequence.join(" ")
if (p == "z"){
console.log(trialPhonemeSequence)
let bearstring = 'b eh1 r z'
console.log(`String|${dictionary['bears']}| length ${dictionary['bears'].length} should equal |${stringified}| length ${stringified.length}: ${dictionary['bears'] == stringified} and ${bearstring == stringified}`);
}
if (stringified == "b eh1 r z"){ //THIS IS WHERE ITS BROKEN
console.log("Bears stringified searching!!!!!!!!!!!!")
}
let hash = stringified.hashCode(dictKeys.length * 4)
if (hashMap[hash] !== undefined)
{
hashMap[hash].forEach((o) =>
{
let key = getObjectsKey(o)
if (checkIfIdentical(dictionary[key], stringified))
{
matches.push(key)
}
})
}
})
}
console.log("Matches", matches)
return matches
}
EDIT (SOLVED):
There is a char 13 (Carriage Return) in Stringified string but not the others. I think I understand where this is coming from. I was inserting a new phoneme with splice in each syllable of the word, and when splicing it onto the end of the words, it's not automatically stripping the '\n', which results in comparison errors. I now know one has to do this manually and wrong hash values. BTW the phoneme dictionary ishere
Thanks #VLAZ !
stringified.split("").map(c => {
console.log(c.charCodeAt(0))
})
console.log("New word")
bearstring.split("").map(c => {
console.log(c.charCodeAt(0))
})
console.log(stringified==bearstring)

convert number to string returns empty if number 0

Trying to convert string to a number, works fine apart from when the number is zero it returns an empty string;
I understand 0 is false, but I just need a neat way of it returning the string "0"
I'm using:
const num = this.str ? this.str.toString() : '' ;
I even thought of using es6 and simply ${this.str} but that didn't work
Because 0 is "false-y" in JavaScript, as you've already figured out, you can't utilized it in a conditional. Instead, ask yourself what the conditional is really trying to solve.
Are you worried about null / undefined values? Perhaps this is better:
const num = (typeof this.str !== "undefined" && this.str !== null) ? this.str.toString() : "";
Odds are you really only care if this.str is a Number, and in all other cases want to ignore it. What if this.str is a Date, or an Array? Both Date and Array have a .toString() method, which means you may have some weird bugs crop up if one slips into your function unexpectedly.
So a better solution may be:
const num = (typeof this.str === "number") ? this.str.toString() : "";
You can also put your code in a try catch block
const num = ''
try {
num = this.str.toString();
} catch(e) {
// Do something here if you want.
}
Just adding to given answers - if you do:
x >> 0
you will convert anything to a Number
'7' >> 0 // 7
'' >> 0 // 0
true >> 0 // 1
[7] >> 0 // 7
It's a right shift bit operation. You can do magic with this in many real life cases, like described in this article.
In my case, the zero (number) that I wanted to converted to a string (which was the value of an option in a select element) was a value in an enum.
So I did this, since the enum was generated by another process and I could not change it:
let stringValue = '';
if (this.input.enumValue === 0) {
stringValue = '0';
} else {
stringValue = this.input.enumValue.toString();
}

Evaluate dynamically user-added If-Statements

How can I achieve that users can add multiply custom if-statements?
For example let's say there is a given variable called x with a given value of let's say 8.
The user sees that x = 8 and has a button to add an if-statement. He clicks the button and can insert the condition which triggers an event (let's say it prints "Hello World"). So he enters "x < 100" into the field which is true. Therefore "Hello World" is printed.
After clicking the button once again, he is able to add an other condition, let's say "x < 7" which is also true. Because both conditions are true, "Hello World" is still printed.
I think you got the point of my questions, even though I lack the vocabulary.
So how could I manage to let user add an undefined amount of conditions which will be checked before "Hello World" is printed?
The only solution I know is to limit the possible amount of conditions and check each one if it is empty / what the conditions says.
Thanks a lot!
Unless you want to build an entire language you have to get clear on what exact operations you are going to allow here.
For example the operation of < and > and ==, basically all comparison operations (<= and >= as well) can be implemented via the following:
/* your X variable, might be var if you desire to change */
let x = 12
/* the array of conditions the user entered */
var conditions : [(((Int, Int) -> Bool), Int)] = []
/* some user input - read as e.g. "x > 2"*/
conditions.append((<, 100))
conditions.append((>, 2))
conditions.append((==, 12))
/* you evaluate all conditions in the following way */
let eval = conditions.map { $0(x, $1) }
let allTrue = !eval.contains(false)
/* allTrue would be true in this case because 12 < 100 && 12 > 2 && 12 == 12 */
Your "hard" job is it now to interpret the user input as some condition. But that is not too difficult, you simply need a mapping of the text input of "<" to the actual operator <.
You can adjust the above code to take care of Double instead of Int easily if you fell like you need that. But you have to aware of floating point inaccuracy and the problem that arise when checking for equality (thanks to #dfri for pointing this out).
A little bit more difficult part comes in regards to combining the conditions with or instead of and what above code does and what you currently describe in your question.
Just because I like closures: The following is the entire input reading and parsing:
func getOperator(str: String) -> ((Int, Int) -> Bool)? {
switch str {
case "<":
return (<)
case ">":
return (>)
case "==":
return (==)
case "<=":
return (<=)
case ">=":
return (>=)
default:
return nil
}
}
func parseUserInput(str:String) -> (((Int, Int) -> Bool), Int) {
var input = str as NSString
input = input.stringByReplacingOccurrencesOfString(" ", withString: "")
//let variable = input.substringToIndex(1) // in case you want more than one variable, but that will have to change the entire setup a bit
// this has to be this "ugly" to incorporate both 1 char and 2 char long operators
let operato = input.substringFromIndex(1).stringByTrimmingCharactersInSet(NSCharacterSet.alphanumericCharacterSet())
let number = input.substringFromIndex(operato.lengthOfBytesUsingEncoding(NSASCIIStringEncoding) + 1)
if let number = Int(number), op = getOperator(operato) {
return (op, number)
}
return ((<, 999999)) // need some error handling here
}
conditions.append(parseUserInput("x > 123"))
Instead of resolving the operator using a function you can even use a plain old dictionary mapping from ">" to (>) etc.
First you need a way to switch between operators. A very simple enum is perfect for this. Just add all the operators you want to use.
enum Operator : String {
case biggerThan = ">"
case smallerThan = "<"
case equal = "=="
init?(string:String) {
switch string {
case ">" :
self = .biggerThan
case "<" :
self = .smallerThan
case "==" :
self = .equal
default :
return nil
}
}
}
Each time a user clicks a button and inserts a condition, a corresponding Condition value will be created.
struct Condition {
var value: Int
var operation: Operator
}
This function returns a Bool depending on x, the inputValue and the chosen operator.
func checkCondition(x: Int, condition: Condition) -> Bool {
switch condition.operation {
case .biggerThan :
return condition.value > x
case .smallerThan :
return condition.value < x
case .equal :
return condition.value == x
}
}
This does the same but for a whole bunch of conditions. Here you can implement more logic. If all need to be true for example add : if !result { return false }.
func checkAllConditions(x:Int, conditions: [Condition]) {
for condition in conditions {
let result = checkCondition(x, condition: condition)
print(result)
}
}
Now all you need to do is store conditions in an array as the user creates them
func userCondition(operation:String, input:String) -> Condition? {
guard let op = Operator(string: operation) else {
return nil
}
guard let doubleValue = Double(input) else {
return nil
}
return Condition(value: Int(doubleValue), operation: op)
}
let conditionA = userCondition("<", input: "10")! // use if let instead of !
let conditionB = userCondition(">", input: "10")! // use if let instead of !
let conditionC = userCondition("==", input: "23")! // use if let instead of !
var x : Int = 23
checkAllConditions(x, conditions: [conditionA,conditionB,conditionC])
struct MyConditions {
let myEps: Double = 0.001
var x: Double
var lessThan = [Double]()
var equalTo = [Double]()
var greaterThan = [Double]()
init(x: Double) {
self.x = x
}
mutating func addConstraint(operand: Double, op: String) {
if op == "<" {
lessThan.append(operand)
}
else if op == "==" {
equalTo.append(operand)
}
else if op == ">" {
greaterThan.append(operand)
}
}
func checkConstraints() -> Bool {
for op in lessThan {
if !(x < op) {
return false
}
}
for op in equalTo {
if !(x - myEps < op && x + myEps > op) {
return false
}
}
for op in greaterThan {
if !(x > op) {
return false
}
}
return true
}
}
Tests:
func feasibleHelloWorld(x: MyConditions) {
if x.checkConstraints() {
print("Hello world!")
}
}
var x = MyConditions(x: 8)
x.addConstraint(100, op: "<")
x.checkConstraints() // true
feasibleHelloWorld(x) // Hello world!
x.addConstraint(8, op: "==")
x.checkConstraints() // true
feasibleHelloWorld(x) // Hello world!
x.addConstraint(7, op: "<")
x.checkConstraints() // false
feasibleHelloWorld(x) // ... nothing

iterate digits in a number as long as its the same number

I need to loop through digits of a number , until there is a difference in thats number , and then output the number length :
For example :
0000123
so the output should be 3.
(eliminate 0000 and output 123)
How can I tackle it ?
Try looping on string until you find difference:
var num="001234",i,max=num.length,output;
for(i=1;i<max;i+=1){
if(num[i]!== num[i-1]){
output = num.substring(i,max);
break;
}
}
alert(output);
You can use ES5 features like filter:
var n = '11112213133';
var test = true;
console.log(String(n).split('').filter(function(v,i,a) {
test = test? v == a[0] : test;
return !test;
}).join('')); // 2213133
You can also use simple string methods (which are probably hugely faster):
var n = '11112213133';
var i = 0, c = n.charAt(0);
while (c == n.charAt(++i)) {}
console.log(n.substr(i)); // 2213133
Though these will trim the first number whether it's repeated or not.

Ordinal string compare in JavaScript?

In javascript:
"Id".localeCompare("id")
will report that "id" is bigger. I want to do ordinal (not locale) compare such that "Id" is bigger. This is similar to String.CompareOrdinal in C#. How can I do it?
I support the answers given by Raymond Chen and pst. I will back them up with documentation from my favorite site for answers to JavaScript questions -- The Mozilla Developer Network. As an aside, I would highly recommend this site for any future JavaScript questions you may have.
Now, if you go to the MDN section entitled String, under the section "Comparing strings", you will find this description:
C developers have the strcmp() function for comparing strings. In JavaScript, you just use the less-than and greater-than operators:
var a = "a";
var b = "b";
if (a < b) // true
print(a + " is less than " + b);
else if (a > b)
print(a + " is greater than " + b);
else
print(a + " and " + b + " are equal.");
A similar result can be achieved using the localeCompare method inherited by String instances.
If we were to use the string "Id" for a and "id" for b then we would get the following result:
"Id is less than id"
This is the same result that Yaron got earlier when using the localeCompare method. As noted in MDN, using the less-than and greater-than operators yields similar results as using localeCompare.
Therefore, the answer to Yaron's question is to use the less-than (<) and greater-than (>) operators to do an ordinal comparison of strings in JavaScript.
Since Yaron mentioned the C# method String.CompareOrdinal, I would like to point out that this method produces exactly the same results as the above JavaScript. According to the MSDN C# documentation, the String.CompareOrdinal(String, String) method "Compares two specified String objects by evaluating the numeric values of the corresponding Char objects in each string." So the two String parameters are compared using the numeric (ASCII) values of the individual characters.
If we use the original example by Yaron Naveh in C#, we have:
int result = String.CompareOrdinal("Id", "id");
The value of result is an int that is less than zero, and is probably -32 because the difference between "I" (0x49) and "i" (0x69) is -0x20 = -32. So, lexically "Id" is less than "id", which is the same result we got earlier.
As Raymond noted (and explained) in a comment, an "ordinal" non-locale aware compare is as simple as using the various equality operators on strings (just make sure both operands are strings):
"a" > "b" // false
"b" > "a" // true
To get a little fancy (or don't muck with [[prototype]], the function is the same):
String.prototype.compare = function (a, b) {
return ((a == b ? 0)
? (a > b : 1)
: -1)
}
Then:
"a".compare("b") // -1
Happy coding.
Just a guess: by inverting case on all letters?
function compareOrdinal(ori,des){
for(var index=0;index<ori.length&&index<des.length;index++){
if(des[index].charCodeAt(0)<ori[index].charCodeAt(0)){
return -1;
break;
}
}
if(parseInt(index)===des.length-1){
return 0;
}
return 1;
}
compareOrdinal("idd","id");//output 1
if you need to compare and find difference between two string, please check this:
function findMissingString() {
var str1 = arguments[0];
var str2 = arguments[1];
var i = 0 ;
var j = 0 ;
var text = '' ;
while(i != (str1.length >= str2.length ? str1.length : str2.length )) {
if(str1.charAt(i) == str2.charAt(j)) {
i+=1 ;
j+=1;
} else {
var indexing = (str1.length >= str2.length ? str1.charAt(i) : str2.charAt(j));
text = text + indexing ;
i+=1;
j+=1;
}
}
console.log("From Text = " + text);
}
findMissingString("Hello","Hello world");

Categories

Resources