assign result of comparison to variable javascript - javascript

If I do that:
var importance_not_matched = {{item.this_article_importance}} <= article_importance;
It seems to work but in my IDE (Pycharm) I have errors:
"Expression expected" on the "<="
and
"underterminated statment" on the final ";"
I'm using flask, item.this_article_importance is an int
I solved my issue by changing to:
year_not_matched = {{item.this_article_year <= article_year}};
I don't understand why it's better yet though

Depending on the type of value that is {{ item.this_article_importance }} you may have to use filter |safe, so that django does not escape it.
Something like this:
var importance_not_matched = {{ item.this_article_importance|safe }} <= article_importance;
You can verify that both variables really have the desired value, even the type, to ensure where the error comes from.
var this_article_importance = {{ item.this_article_importance|safe }};
console.log(typeof(this_article_importance), this_article_importance);
It's hard to know more without more information. You can review this question where the topic is discussed.

I solved my issue by changing to:
year_not_matched = {{item.this_article_year <= article_year}};
I don't understand why it's better yet though

Related

Mirth - Add new field to OBR 16 segment

Got an opportunity to work in Mirth to add an entry in the OBR field.
With the help of this forum, I was able to edit an existing data, that works perfectly fine.
But failing to add a data to a field which doesn't exist in the source HL7.
Below is the example,
SourceHL7
PV2|||||||System Alert Off~0437689973~ABC-KOTHAI-AUS
OBR|1||ABCDEDFGH|754051^ABCEDEF^MDC|||20190225133500+0000||||||||||||||||||F
In the DestinationHL7, I want to check if PV2.7.2 has "KOTHAI", if yes, then update the OBR.16 as below
OBR|1||ABCDEDFGH|754051^ABCEDEF^MDC|||20190225133500+0000|||||||||KOTHAI|||||||||F
With the below Javascript, I am able to see the last value in OBR is changed as M but no "KOTHAI" is available. I could see the change in Transformed data but not in Encoded data. Could you let me know what am I doing wrong.
tmp=msg;
var code = tmp['ORU_R01.PATIENT_RESULT']['ORU_R01.PATIENT']['ORU_R01.VISIT']['PV2']['PV2.7'][2].toString();
if (code.indexOf("ARSTALL") != -1 )
{
tmp['ORU_R01.PATIENT_RESULT']['ORU_R01.ORDER_OBSERVATION']['OBR']['OBR.25'] = "M";
tmp['ORU_R01.PATIENT_RESULT']['ORU_R01.ORDER_OBSERVATION']['OBR']['OBR.16'] = "KOTHAI";
}else {
tmp['ORU_R01.PATIENT_RESULT']['ORU_R01.ORDER_OBSERVATION']['OBR']['OBR.25'] = "F";
}
Here is the same answer I gave on your forum post http://www.mirthcorp.com/community/forums/showthread.php?t=218996
You're using the strict parser, so you need to make sure everything is named correctly depending on hl7 datatype.
I think you want to do this:
tmp['ORU_R01.PATIENT_RESULT']['ORU_R01.ORDER_OBSERVATION']['OBR']['OBR.16']['XCN.1'] = "KOTHAI";

Data changes after assigning to another variable

I work with Discord.js User object and Mongoose Schema. But the problem doesn't seems to be part of those.
var Message = require('../app/models/message'); //Mongoose Schema
...
var newMessage = new Message();
...
//taggedUser is an object containing all the info about user. id property contains user id which is number.
const taggedUser = message.mentions.users.first();
newMessage.message.to = taggedUser.id;
console.log(taggedUser.id);
console.log(newMessage.message.to);
The code above should assign user ID to Schema. Everything works, but...
442090269928849410
442090269928849400
Last 2 characters aren't the same among these variables now. How is this even possible? The = changed the actual data inside the variable?
In case it is Mongoose here is how Schema looks like:
var msgSchema = mongoose.Schema({
message : {
from : Number,
to : Number,
content : String,
time : Date
}
});
Edit:
If I change
to : Number,
to string:
to : String,
It works properly. I still need the answer on why does this work incorrectly with number. Right above the problematic line I have another id which works perfectly fine:
newMessage.message.from = msg.author.id;
I have already tried to parse taggedUser.id to integer or creating Number() object but that didn't help. So every time I turn taggedUser.id into a Number or parse it to int it changes to the slightly different number.
I don't know what to think. How can data change during the assignment?
If there is not enough data provided in the question please ask me and I'll add everything needed. I can't imagine what might be causing this bug.
9007199254740992 - Highest safe number in JS
442090269928849410 - Your integer (id)
The reason of that small variation is the 'Max precision' JavaScript can work with.
When you tried to use the id as a number it was affected by this and it changed because JavaScript can't be that precise.
If you see both numbers at the beginning of this answer you can see that they are separated by 2 characters, that is why only the 2 last character changed.
Basically your integer was affected by the max precision JS numbers can have.
Reference: https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Number/MAX_SAFE_INTEGER
You might just be seeing an artifact of console.log running asynchronously. Try this:
console.log('' + taggedUser.id);
console.log('' + newMessage.message.to);
...and see if that makes any difference.

Angularjs expression is not working for ng-bind and {{ }}

Angularjs addition is not working fro ng-bind and {{ }} but multiplication is working why?
I have the following code shown below
single_style.thread = 3;
single_style.stiching = 5;
and:
1) <td>{{single_style.thread + single_style.stiching}} </td>
2) <td>{{single_style.thread * single_style.stiching}}</td>
1) First i getting answer as 35
2) second i getting answer as 15
Even i use ng-bind its also not working why?
Update:
The problem was as suggested, you were trying to add strings which resulted in concatenation, After further reading and this great SO post shown below.
SO Answer
which says
Angular does not use JavaScript's eval() to evaluate expressions. Instead Angular's $parse service processes these expressions.
Angular expressions do not have access to global variables like window, document or location. This restriction is intentional. It prevents accidental access to the global state – a common source of subtle bugs.
So we can't use parseInt in angular brackets, we can either move the calculation inside a javascript function and call it from angular brackets. My Solution is, since (*) multiplication operator is doing type conversion, just multiply the variable by 1 so that you will get the same number and also convert the datatype to number. Then the calculation will be done as expected. Please let me know if this fixes your issue. The html will be as so.
<td>{{single_style.thread*1 + single_style.stiching*1}} </td>
Plunkr Demo
I think in your scenario the variables are strings. The + works as two ways, as an addition operator for numbers and as a concatenation operator for strings, so in your question, the first scenario, the operator is working as a concatenator and appending 3 and 5 as 35, for your second case, I think multiply operator (*) is doing type conversion before performing operation thus the correct calculation takes place (15), whereas in the first case type conversion is not taking place!
<td>{{parseInt(single_style.thread) + parseInt(single_style.stiching)}} </td>
<td>{{parseInt(single_style.thread) * parseInt(single_style.stiching)}}</td>
I think we miss something in your example.
+ operator concatenates strings '3' and '5' when * converts to int and makes multiplication. The same thing will woek for - and /
[EDIT]
Modify your data before you render it:
$scope.all_value = [];
angular.forEach(data, function(item){
item.thread = parseFloat(item.thread);
item.measure = parseFloat(item.measure);
item.bulk_qty = parseFloat(item.bulk_qty);
item.price = parseFloat(item.price);
item.pack_charge = parseFloat(item.pack_charge);
item.label = parseFloat(item.label);
item.elastic = parseFloat(item.elastic);
item.button = parseFloat(item.button);
item.markt_price = parseFloat(item.markt_price);
item.stiching = parseFloat(item.stiching);
$scope.all_value.push(item);
})
working example Demo
working example
$scope.single_style ={
thread: 3,
stiching: 5
};
and:
<p>{{single_style.thread + single_style.stiching}} </p>
<p>{{single_style.thread * single_style.stiching}}</p>
Output
8
15
Suppose your case when value is defined as string:
$scope.single_style ={
thread: '3',
stiching: '5'
};
and:
<p>{{single_style.thread + single_style.stiching}} </p>
<p>{{single_style.thread * single_style.stiching}}</p>
Output
35
15
Solution
The better way is to convert string to int on controller level, a.e.:
$scope.single_style ={
thread: parseInt('3'),
stiching: parseInt('5')
};
Demo

What is the ??! operator in Javascript?

when I'm looking for some sites Javascript code, I see this
function hrefLeftMenu() {
var x = true;
for (i in info) {
$(".leftmenu ul").append("<li" + (x ? " class='active'" : "") + " onclick='openAnInfo(\"" + i + "\", this);'> - " + info[i].title + "</li>");
x = x??!x;
}
openAnInfo("0", ".lelelesakineyy");
}
What it does in javascript? Why the coder's used this operator?
Thanks.
What it does in javascript?
It throws a syntax error.
> x = x??!x;
SyntaxError: Unexpected token ?
Why the coder's used this operator?
Taking a reasonable guess (beyond "they made a mistake") would need more context. Saying for sure would require mind reading :)
In JavaScript this is not valid code. However, the sequence ??! exists (existed?) in C as a trigraph, representing |. Maybe that's not JavaScript code, or it was poorly-ported from ancient C code. But even then, x = x | x can hardly be called a useful statement.
EDIT: With a bit context in the question now, that speculation is likely wrong. A friend suggested that maybe the sequence x?? was a typo and subsequent attempt to correct it where backspace was turned into a character by some intermediate mangling (not uncommon when typing in terminals or via SSH) and that the line in question was supposed to be x = !x.
I think it is a mistake. They're generating a menu and x is used to set an item as active, and it looks like they want to default to selecting the first item. They want x to be true the first time around, and then false for the rest. It was probably supposed to be something like
x = x?!x:x; // If first time through, then set x = false for the rest
aka
x = false; // Set x = false for the rest
but confusion/muddy thinking led to over-complification.
Was this a mistake?
Did you mean this?
x= x?x:!x;

Is it safe to run code inside the conditional operator?

I often see and use codes like:
var myvar = (1 < 2) ? 3 : 4 ; //if 1 < 2 then myvar = 3, else = 4
But I just recently saw a code that was executing code, just like some kind of replacement for the if(){}else{}:
Example:
(1 < 2) ? alert("example1") : alert("example2");
The first thoughts that came to me were, "wow, this is like 6-7 characters shorter", "endless of possibilities" or "this made my day".
My question:
Is this thing error-free and safe to use? (like, with a lot of code inside, and nested stuff)
For now, I will just keep using it in the normal way, I have the fear that if I start using it to execute pieces of code might not work.
Is this thing error-free and safe to use? (like, with a lot of code
inside, and nested stuff)
Yes. However, the more code that's within it, the less readable it becomes.
I prefer to use it (the conditional operator) for short, concise statements. Anything more complex deserves an if/else for the sake of readability and maintainability.
There are some exceptions. You can't do this with:
break
continue
Any block like if, for, while, do, or try
for example. What's more, it can mess with your order of operations:
x < 3 ? l = true : r = true; // Syntax error, = has lower precedence than ?:
But that's not the reason not to do it, it's because it's ugly. Which one is clearer to you, this:
if(i > 5) {
alert('One');
} else {
alert('Two');
}
or
i > 5 ? alert('One') : alert('Two');
? It's not quite right, is it? And saving characters is never a reason to do anything, really; otherwise there would be no comments or whitespace. A good minifier like Google Closure Compiler will automatically convert these for you when possible, and there are plenty of other places to save. In the end, it's just whatever you find most convenient and readable.
Also, if you do end up needing break, continue, etc. then it's going to be rather inconsistent and unattractive code.
You're referring to the ternary operator. It's usually used for setting variables with simple strings like this:
var phone = old ? "blackberry" : "iPhone"
That much simpler than using an if:
var phone = "iphone"
if (old) {
phone = "blackberry"
}
It's good in this context, in the example you described and as soon as it starts getting confusing or I'd definitely not recommend it!
Your example might be made better like this:
var msg = 1 < 2 ? "alert1" : "alert2";
alert(msg);
You could also write:
alert( 1<2? "example1" : "example2" );
The ternary opertator is designed for simple cases, sometimes developers get carried away and use it to replace multiple if..else statements, e.g.
var someVal = foo < bar? 'yes' : bar > fum? : fum : fi != fee? fi : fee;
which is not a good idea IMHO.

Categories

Resources