Boolean value in javascript - javascript

Ok, I've got an issue I don't understand.
I have a boolean value which I test and if true I do something.
BUT javascript never go in it even if the var is true.
I try this :
if(isConfigD)
handleConfigurationD;
this :
if(isConfigD == true)
handleConfigurationD;
And this :
if(isConfigD === true)
handleConfigurationD;
But nothing work whereas isConfigD is always set on true :(
What am I missing ?

You condition works well, but if you call a function, then you need parenthesis for the call.
handleConfigurationD();
// ^^

handleConfigurationD is just an identifier. That statement is going to have one of two outcomes:
A ReferenceError
"Yup, that's a bit of data"
Presumably you have stored a function in it and want to call the function.
handleConfigurationD();

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

Short circuit in JS stopping on first input

I'm having some problems trying to use short circuiting on a web page I am making.
I am trying to use
document.webkitExitFullscreen() || document.mozCancelFullScreen() || document.exitFullScreen();
But it seems to stop on the first try, despite that I would have though it would continue after the first argument comes up as undefined.
If I simply type in
document.mozCancelFullScreen()
then it works fine
I was wondering if anyone could point me to what I'm doing wrong here
The screenshot is taken in firefox btw.
Thanks in advance
Your code is trying to call document.webkitExitFullscreen and if it returns a falsy value, call document.mozCancelFullScreen, etc.
But if document.webkitExitFullscreen itself is undefined you'll get an error trying to call it, and the code will stop running at that point.
Perhaps:
var exitFullScreen = document.webkitExitFullscreen || document.mozCancelFullScreen || document.exitFullScreen;
if (exitFullScreen) {
exitFullScreen.call(document); // Just `exitFullScreen();` may work as well
}
Or alternatively:
["webkitExitFullscreen", "mozCancelFullScreen", "exitFullScreen"].some(function(name) {
if (document[name]) {
document[name]();
return true;
}
});
...which avoids the whole "do I need call or not?" issue.
The problem is that you're already calling the function, so if it doesn't exist, you get an error.
You coudl try something like:
(document.webkitExitFullscreen || document.mozCancelFullScreen || document.exitFullScreen)();

Boolean in IF Condition causing Errors in JavaScript

I am developing a webpage with fairly simple JavaScript. My entire JavaScript code is:
function showUnlockPopup(isViolated,instId,unlock_done,unlock_total,period,endDate){
alert(isViolated);
if(isViolated){
if(unlock_done < unlock_total){
showInfoPopup(instId,unlock_done,unlock_total,period);
} else {
showNoUnlocksPopup(instId,unlock_done,unlock_total,period,endDate);
}
} else {
showNotViolatedModal();
}
}
However, irrespective of the value of the 'isViolated' variable, my 'showInfoPopup' function is called.
I have checked the code for the method call too:
<button onClick="showUnlockPopup(isViolated)">Unlock</button>
The alert shows 'true' or 'false' values correctly, but the logic runs for only 'true' irrespective of the value.
I think I am missing something fairly basic here. I tried a lot, but of no use. Kindly help me with the same.
it is because isViolated is returned as a string. so unless the string is null it will be true if it has some contents. You should change your check to isViolated == 'true', make a new variable that is a boolean and assign it depending on isViolated or something third.

Forcing JavaScript return false to persist through functions

Thanks for taking time to review this question. I've been trying to fix a problem for one or two hours with no success...
I have a web page that sets a JavaScript variable based on the response from a function:
grade = getScore(questionAnswer, userAnswer, questionType);
(userAnswer is the user's answer to a question and is retrieved from a textarea)
Here is getScore:
function getScore(questionAnswer, userAnswer, questionType) {
switch(questionType) {
case 'multiplechoice':
return scoreMC(questionAnswer, userAnswer);
break;
case 'usertypesanswer':
return scoreTA(questionAnswer, userAnswer);
break;
default:
return 0
}
}
The functions for scoreMC and scoreTA have been tested thoroughly and work great. The issue is that if a user's answer is not formatted correctly, scoreMC or scoreTA will return false. Otherwise it returns the values score and msg. However, instead of getting a "false" value for "grade" when I set the value of the grade variable based on the getScore function, I get "undefined". (We have no problems when the user response validates properly.)
After setting "grade", I have tried to check if any part of it is undefined:
if(typeof(grade.score) !== undefined)
I do not understand why, but even when I see "undefined" in my Firebug console, grade.score passes this check...
Does anyone see what I am doing wrong? Thank you very much for your assistance. I have a lot to learn about JavaScript.
if(typeof(grade.score) !== undefined)
can be
if(grade.score && grade.score !== false) // if I understand your question
or
if(typeof(grade.score) !== "undefined")
typeof returns a string
If no return statement is used (or an empty return with no value), JavaScript returns undefined.
It is almost certain that one of your score functions (scoreMC, scoreTA, whose code you should have included in the question) does not return a value i.e.
return;
Or just reaches the end of the function code block without encountering a return.

Categories

Resources