js doesn't execute 'else' statement - javascript

I have an if-else statement in JavaScript which returns false, but doesn't execute the code inside 'else'.
part of the script inside the cshtml file:
if (#Session["mission"].ToString() != "1") {
setInterval(function () { myTimer(ctxPoints, ctxLines); }, 1000 / #ViewBag.rate);
} else {
alert("hi");
}
In the debug the '#Session["mission"].ToString() != "1"' statement returns false, but nothing jumps to the screen.

You should open your web browser Developer console and see the real script which reaches the browser.
The Razor parts, #Session["mission"].ToString() doesn't exists in the browser script, because they are evaluated in the server, before sending them to the browser. So it makes no sense to try #Session["mission"].ToString() != "1" this in the console. In the browser you will get something like:
if (1 != "1")
or even like
if ( != "1")
which would provoke an error.
JavaScript does coalescing, so 1 is equal to "1" when you use the comparers == or !=, so you don't need to include the qoutes around the value (unless you get the error mentioned above: in this case, if you included the quotes, you'd get if ("" != "1")).
Coalescing is avoided by using the !== or === which would consider 1 not equal to "1", so, including the quotes doesn't make any difference in this case.

I think you should clear Js code and c# code
if ("#Session["mission"].ToString()" != "1")
Session["mission"].ToString() this print string in c#, but you need to encapsulate it in quote to make this value get type string

Related

else operator | JS | Chrome extention

I have this function:
function FizzBuzz(){
if(document.getElementById("textbox") == true){
Fizz();
} else {
Buzz();
}
}
The Fizz() and Buzz() functions place text in textboxes specified by document.getElementById in their own functions.
in it's current configuration, it should to my understanding execute Fizz() if ("textbox") is found on the webpage, otherwise Buzz() should be executed. This does not happen, and it will only execute one of them, no matter if ("textbox") is true or not.
The ("textbox") referenced above only exists on one of the two pages this is designed to work with.
Changing to !== true will invert the effect, the same with == false, as with the current behavior is expected. I have also tried to check for == null and !== null, this results in the same behavior.
I simply do not understand what I'm doing wrong here.
document.getElementById returns either null or element. So, comapring both with == or === with true or false will always return false.
You can directly put document.createElement inside if as a condition.
If you still face the issue, I suggest you to put a break point and see how the code is executed.
Sample code for better understanding - https://codepen.io/Yash__/pen/WNzgYvL?editors=1111
<h1 id="hai">hai</h1>
if(document.getElementById('hai')){
console.log("there");
}else{
console.log("not there")
}

why my while loop doesn't work in Jmeter?

please click the link to view the images.
I have a Json Extractor, which saves the value returned in "status"
it's working:
and I also have a "Controller while" with this condition:
$ {__ javaScript ("${status}". indexOf ("string") != 500 ,)}
But something strange is happening
2021-11-25 14:30:46,258 DEBUG o.a.j.c.WhileController: Condition string: '$ {__ javaScript ("ERROR". indexOf ("string") != 500 ,)}'
2021-11-25 14:30:46,258 DEBUG o.a.j.c.WhileController: Condition value: 'false'
2021-11-25 14:30:46,258 DEBUG o.a.j.t.p.AbstractProperty: Running version, executing function
Wasn't it supposed to give "true" and end the loop?
Your condition is not very correct, if you want to end the loop when your ${status} variable becomes 500 you should use something like:
${__javaScript("${status}" != "500",)}
Also from performance perspective it's better to consider using __jexl3() function like:
${__jexl3("${status}" != "500",)}
or __groovy() function like:
${__groovy(vars.get("status") != "500",)}
as this is something that __javaScript() function documentation suggests

JS If/Then to Remove NULL Value

I have a very simple JS script that is embedded into an email. The goal is to autopopulate the salutation of the email and is as follows.
if (Contact.Field("firstname") == "NULL") {
document.write(Contact.Field("custname"));
} else if (Contact.Field("firstname") == "") {
document.write(Contact.Field("lastname")"Household");
}
The problem I'm running into is that the script will still autopopulate the NULL value in the email instead of switching to the "custname" field when it runs. I'm sure this is due to user error, but if anyone could help steer me in the right direction I would greatly appreciate it. Thanks!
You have used "NULL" in the if statement, where you should have used null (without quotes, and in lowercase letters.) on it's own, like this:
if (Contact.Field("firstname") == null) {
document.write(Contact.Field("custname"));
} else if (Contact.Field("firstname") == "") {
document.write(Contact.Field("lastname")"Household");
}
Using "NULL" would check if the text in Contact.Field("custname") was the string "NULL", rather than checking if it has no content.
As to whether to use == or ===, you can find an answer here: Which equals operator (== vs ===) should be used in JavaScript comparisons?

Weird Javascript If Statement

I'm not sure whats wrong with my if statement. I'm trying to use my model inside my javascript.
if (#Model !== null)
{
if (#Model.Level !== null)
{
//use #Model.Level
}
}
else
{
//use default
}
The Model is null, but it still steps into the first if statement (and breaks on the second one obviously). I've tried #Model, !#Model and != but it still always steps in.
What am I doing wrong? (It's also got squiggly red lines under both !== saying there is a syntax error)
Triple equations work without type castings in JavaScript. In your case you are might get an undefined object which isn't a null value.
For example:
undefined === null //Do not cast when comparing, increased performance.
false
undefined == null //Do cast when comparing, decreased performance.
true
In addition, if #Model value is null then you won't see a null value on client side. It gives you an empty value like this:
if( == null)
{
}
This will cause an error on your javascript side. Null check should be done at server side. For that reason you have to put # value in front of your code to make it server side:
<script>
#if (Model != null) //Server side code.
{
if (Model.Level != null) //still server side code.
{
<text>
alert("your javascript here"); //write javascript on your screen.
</text>
}
}
</script>
In order to check if something is null or undefined in javascript, use if (#model) rather than if ( #model !== null)
http://appendto.com/2010/10/how-good-c-habits-can-encourage-bad-javascript-habits-part-2/
The reason why it steps into the if statement is because it evaluates to true, no weirdness to be found here. Your browser is not temperamental. For a list comparisons check out this http://dorey.github.io/JavaScript-Equality-Table/
Also note that there is a difference between double and triple =. Triple will type cast
This code looks a LOT like a Razor then Javascript, though you may be trying to mix the two of them together.
Your choices:
1) Convert Model to a JavaScript object using something like this:
Turn C# object into a JSON string in .NET 4
2) Use the Razor if statement and write out your final JavaScript with it.
<script>
// Code assume this is an numeric value
var useThisVariable;
</script>
if (#Model !== null)
{
if (#Model.Level !== null)
{
<script>
useThisVariable = #Model.Level;
</script>
}
}
else
{
<script>
useThisVariable = -1;
</script>
}

Evaluate prompt value wont

I'm very new to JS - only a couple days in.
Trying to write a very basic prompt evaluated by an if statement.
When I run the code below, the user is prompted, but the statement is never evaluated by the if statement.
Any help? -- I realize the answer is probably simple and obvious, but as a SUPER beginner, what do I do?
var bool = prompt("What is an example of a boolean?");
if (typeof(bool) === "boolean") {
print("correct! that is a boolean");
print(bool) ;
};
In this case, assuming the user inputs something in the prompt, the type of the bool variable will always be a string. You'd rather check if the input compares to the string "true" or "false" etc., like this:
if (bool.toLowerCase() == "true" || bool.toLowerCase() == "false") {
...
}

Categories

Resources