I've just come across a situation in which an if statement is constantly triggering despite the parameters not being true.
function draw(){
if(5<mouseX<10 && 5<mouseY<10){
background(100);
} else (background(200));
}
When running the above program, the background is always value 100, no matter where I move the mouse.
I've boiled this down from an attempt to make a button within a mouseClicked function in a successful attempt to find my fundamental problem of misunderstanding. I'm running it in p5js if that helps.
Your if-statement should be as follows:
if(5<mouseX && mouseX<10 && 5<mouseY && mouseY<10)
The reason behind this behaviour is:
if(5<mouseX<10 && 5<mouseY<10){
background(100);
} else (background(200));
When the javascript engine reads 5<mouseX it returns true then reads true<10 this is also true as here the js engine treats true as 1 and condition 1<10 is always true. This happens with other part of the expression also.
Try using :
if(5<mouseX && mouseX<10 && 5<mouseY && mouseY<10)
Related
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")
}
I have the following code in which I ask it to check if either one of 2 dataLayer values is "!= false" then run the rest of the code.
However, code breaks as soon as the first condition fails and does not move to check the second condition. So, how can I let both conditions be checked as one of them will always be available in dataLayer?
function init() {
if (dataLayer.condition[1] != false || dataLayer.condition[2] != false) {
Do something
}
}
Below is the screenshot of the error I get when the first condition values are missing on the page.
You can use optional chaining (?.) for this, if your execution context is expected to support it:
if (dataLayer?.condition[1] != false || dataLayer?.condition[2] != false) {
// Do something
}
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
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)();
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.