I am working on a c++ web-based IDE for beginners where one of it's core function is to let the user know the evaluation result (true or false) of the conditional statement they have on my contenteditable div.
Say i am already able to fetch the "fragment" (what i call it based on my architecture) that i wanted to evaluate and i already know the values of the variables...
Can you please suggest a way on how would i evaluate the text on the fragment as a conditional statement that will return either true or false...
Example
when var b = 5;
$('.frag').eq(1).text() //"if( (b>1) || (b==10) )"
after evaluation(what im asking for help) should return true
$('.frag').eq(2).text() //"(b>1)"
should return true
**$('.frag').eq(3).text() //"(b==10)"
should return false
UPDATE
The problem i am seeing with eval is that if i have a var1 in the contenteditable div and i have a var1 in my code. I should put a header on the variables from the contenteditable then right? like sc_var1 to prevent messing up with var1 from my source code?
You can use Eval like this:
var expression = $('.frag').eq(1).text();
var result = eval(expression);
However I generally do not recommend using eval because there is always a much better workaround.
if I get the bigger picture of your needs I would be able to provide a better solution.
Related
I am doing a ternary operator check to see if a certain element is found on the document and set some style and property which will later modify based on other conditions related to user input.
but if the active page does not have those elements I don't want the javascript though error claiming "cant set disabled for null value".
so I use a ternary operator with an array to set multiple values for the element in the case it is found on the page.
my question is am I doing it right or there is a better way ???
submitCodeContent ? [
(submitCodeContent.disabled = true),
(submitCodeContent.style.backgroundColor = "#886A6C"),
]
: submitCodeContent;
Only use the conditional operator when you need to use the resulting expression. If you just want to perform operations if a particular condition is met, use if/else. Here, you just need
if (submitCodeContent) {
submitCodeContent.disabled = true;
submitCodeContent.style.backgroundColor = "#886A6C";
}
Using the conditional operator when you don't need the result is confusing to readers of the code, and should be avoided.
I have the following code amd it gives me compilation error:
for(var i in test){
(this.watcherFailureCount> 10) && break
}
However the following works:
if(this.watcherFailureCount> 10)
{
break
}
Am I missing anything here? why the first one is not working for me?
The && tries to evaluate your expression and cast its return value to boolean. The break you use is a keyword that controls the loop and should not be used in expressions.
Some languages allow that but it just seems that js doesn’t. And to be fair it is ok not to because is missleading. Imagine conditions like: a && b && break && c && d = a.
There is no real benefit in the first option unless you codegolf or something, and if you codegolf you chosed the wrong language :).
Dont fully understand what youre trying to achieve here however impretty sure the first code snippet is incorrect syntax.
If you want that as an inline if statement try:
if(this.watcherFailureCount>10)break;
However ensure if you are using break that it is inside of some form of code loop like a while or a for loop. And using && with break is invalid as break cannot be a true or false statement so it cant be used like that.
I saw a code fragment like this:
with(document)0[(getElementsByTagName('head')[0] || body).appendChild(createElement(xxx))]
I don't know how to understand with(document)0[]
This is not valid JavaScript syntax and, trying to read around that, the semantics are extremely unclear.
I imagine the author meant something like this:
document.getElementsByTagName('head')[0] || document.body.appendChild(createElement(xxx))
"If there are any <head> tag in the document, return the first. Otherwise return the the result of appending createElement(xxx) to the body".
It is hard to answer this question without the full code. But I'll made some assumptions here.
The first thing that I'd like to say is to avoid of using with() statement. It is not recommended in ECMAScript 5 and is forbidden in strict mode. And one of the reasons is your fragment - this code confused a lot of people, even you.
So let's rewrite it a little bit to make it more understandable:
with(document) {
0[(getElementsByTagName('head')[0] || body).appendChild(createElement(xxx))];
}
How with works you can read here - with statement, but basically it is give us an ability to use directly all the properties and methods of the expression that we are sending to with (in our case it's a document).
So, how this code fragment will looks like without with?
0[(document.getElementsByTagName('head')[0] || document.body).appendChild(document.createElement(xxx))];
The only answer that I don't have is - why to execute this code inside of brackets? The assumption that I have is the following:
this code fragment (document.getElementsByTagName('head')[0] || document.body).appendChild(document.createElement(xxx)) will return the node of new created element. But if we place this code into the 0[], it will return undefined as there is no such property. Again, it's hard to understand all parts of this code fragment without the whole picture.
I am still new a Javascript tried earching and tried the development tool in Chrome, too see if I could find the problem.
Working in Intellij IDEA 13, Java, javascript and xhtml.
My problem is that I have a piece of javascript, then in IDEA when moused over, says that
Expression Expected
the javascript code looks the following
<script type="text/javascript>
function nameOfFunction(){
if(#{trendAnalysisLocationReportController.model.showTargetLine}){
this.cfg.series[this.cfg.data.length-1].pointLabels = {
show: false
};
}
}
<\script>
the method in the if sentence is a java method with a boolean return value.
the error is shown when hovering
'#{'
if Had a look at the following questions, before :
Expected Expression
boolean in an if statement
But didnt get me a solution.
what Iam I doing wrong ?
It looks as though the problem is the part that you've got within the #{...} block. Without knowing the context, it's hard to be sure, but is this something in a view/JSP page that's supposed to be replaced with a property at runtime? The if block will expect the part inside the brackets to be a boolean value, so if that's rendered to either 'true' or 'false' it would execute at runtime but would likely show the error you're seeing in your IDE as it's not actually a valid piece of JavaScript. If, on the other hand, you're expecting to be able to call your Java method/property from your JavaScript code, you're going to need to do something that requests that value from the server-side code - AJAX or similar.
Also worth noting that we can't see what this.cfg is supposed to represent. If that's your entire script block, then there's nothing that defines the cfg object within the current scope.
One last thing, you should change the <\script> end element to as it won't be understood properly by some browsers.
if (1) {
google_conversion_value = 1;
}
What is the meaning of the above statement? I mean, this looks like it will always execute so why bother with the if statement?
updated: one reason might be remnants of scripting on the server side. Any other ideas?
updated2: could as easily change the value of the assignment without bothering with the if statement, no?
There are two likely explanations:
It's a leftover from debugging.
The file containing this code is generated dynamically and the original sourcecode contains something like if(<?php echo $some_stuff_enabled; ?>)
However, in the latter case it would have been cleaner to output that code block only if the condition is met - but maybe it's used in some crappy template engine that just allows replacements but no conditionals...
I've seen this before, and I've always assumed it was a remnant of some old condition that was no longer needed, but never removed. I can't see any actual reason to do something like that otherwise.
Potentially because the person writing the code wanted an easy way to turn it off and on again, this is especially useful if there is a lot of code inside the block (not the case here).
Another possibility is that the original programmer couldn't be bothered writing the logic or, more likely, it hadn't been specified so the "if" was left as a placeholder.
More than likely left in from a debug release or something similar. You're right, it will always execute. It could also have been done like this so that it can be easily enabled / disabled by setting the if to 0. Perhaps the developer intended to use it as a flag somewhere else in the code?
actually, this happens when the "if" condition is driven from server, so instead of doing the right thing and not produce the script when the condition is false, they do something like this:
if (<% if (my_server_condition) then Response.Write("1") else Response.Write("0") %>){
// code goes here
}
Perhaps the if statement used to check for a legitimate conditional, and then someone replaced it with a truthy value for testing/debugging/etc.
You're right, it will always execute because 1 is truthy. I would go through your source control history and investigate that line to see if it used to contain a real conditional. If the conditional was always 1, then it's likely a debugging statement. Otherwise someone might have meant for it to be a temporary change, and may not have meant to check that in (which could be bad).
I'm not sure where this code is from, but as you indicated it will always execute. As for why you'd do this, there are times where you want to see what the result of branch code would be, without having to setup an environment. In this case you can comment out the actual value and replace it with if(1) instead for testing:
// if( ... some hard to achieve condition )
if (1) {
// Now you can see what happens if this value is set quickly
google_conversion_value = 1;
}
Of course the problem with this is that it's sometimes easy to forget to remove the if(1) and uncomment the proper condition.
This is actually the javascript recommended by Google on http://support.google.com/adwords/bin/answer.py?hl=en&answer=1722054#nocomments (click on Step 2 for the sample HTML)