Javascript if statement in RPG - javascript

I'm learning about canvas by making a simple RPG.
I'm trying to make it so that if you walk into a certain area, it runs a function. So I used an if:
if (x<150, x>50, y<150, y>50) {
(code I want to execute)
}
But even when the statement is false, it's still considered true. I want it so that all statements in the parentheses must be true for the code to execute. Any help?

Use &&:
if (x < 150 && x > 50 && y < 150 && y > 50) {
// (code I want to execute)
}
Or to separate each part for readability:
if ((x < 150) && (x > 50) && (y < 150) && (y > 50)) {
// (code I want to execute)
}
To learn more, check out:
Logical Operators

Use &&, not ,:
if (x<150 && x>50 && y<150 && y>50) {

Using && means you want all of the conditions between the brackets evaluated. The code below will be executed if x is less the 150 AND y is less than 150 AND x is greater than 50 AND y is greater than 50. If only 3 of the conditions are satisfied, the code will not run. As a result your new code will look like this:
if (x < 150 && x > 50 && y < 150 && y > 50) {
// (code I want to execute)
}
Using || basically means or, and the code gets executed if any of the conditions are valid. The code below will be executed if x is less the 150 OR y is less than 150 OR x is greater than 50 OR y is greater than 50. In effect, if even 1 of the conditions is met the code will run.
if (x < 150 || x > 50 || y < 150 || y > 50) {
// (code I want to execute)
}
You can find a simple tutorial here

Related

If a<x<b and C<y<d and x>y return value

Can some one explain if this is correct in JavaScript?
Or how to achieve that?
X - constant imput
y - constant imput
X>Y
If ay return value
Option 1: if (100=<x=<500 & 1=<y=<7) return 5;
Or
Option 2: if (100<=x & x=<5000 & 12<=y & y<17) return 5;
This should work:
if (x >= 100 && x <= 5000 && y >= 12 && y<17) return 5;
To be able to return something, you need to use this statement within a function. Otherwise, you need to define what you want to do with the 5.

Ignore if x < 50, unless x = 100. How would this be done?

I am trying to make it something like:
if (x > 50) return unless {
if (x = 100) {
console.log("x is equal to 100");
}
I know that is not at all how you are supposed to use unless, but I am trying to make it so that if x is over 50, then it should be ignored unless it is 100, and if it is 100, then it should run a command.
You don't quite describe what you want to do in the various cases, so here's the most generic code:
if (x == 100)
{
// do whatever you want to do when x is 100
}
else if (x > 50)
{
// do whatever you want to do when x is (strictly) greater than 50, but not 100
}
else
{
// do whatever you want to do in all other cases, i.e. when x is less than or equal to 50
}
Try this:
if (x > 50 && x == 100){
console.log("x is equal to 100");
}
Since you say that everything over 50 should be ignored, presumably you care about x < 50. In that case, you can use this logic:
if (x < 50) {
// Not ignored
} else if (x == 100) {
// Special case
}
What you're trying to do doesn't make sense. If all you need to know is if x is 100, then nothing else matters. Who cares if x is greater than 50 if you're not handling it.
if(x == 100){
console.log("X is 100");
}

JS Logical And / Or

Can someone explain to me why both of these alert 'True'? The logical OR makes sense I think - The OR short circuits after the first test: 100 != 90, therefor it is true. By why does the AND short circuit after the first test and not keep going?
if (100 != 90 && 100 && 190 && 200 && 290){
alert("true");
} else {
alert("false");
}
if (100 != 90 || 100 || 190 || 200 || 290){
alert("true");
} else {
alert("false");
}
EDIT: THANK YOU EVERYONE FOR YOUR HELP! BASED ON YOUR SUGGESTIONS, I THINK I WILL AMMEND MY CODE TO THIS:
var trains = [90,100,190,200,290];
var testValue = 100;
if (trains.indexOf(testValue) >= 0){
alert("true");
}
This will evaluate to true:
if (100 != 90 && 100 && 190 && 200 && 290)
Because each part is truthy. Any non-zero value in javascript is consider to be equivalent to true. So you have:
if (true && true && true && true && true)
Which is, obviously, true. It's not short circuiting, because none of the values are false.
I suspect the mistake you've made is to think that this:
if (100 != 90 && 100)
Is doing the same thing as this:
if (100 != 90 && 100 != 100)
Which is simply not how the syntax works. It's easy to say in words "if 100 doesn't equal 90 and doesn't equal 100" and understand what that means, but in javascript (and most other languages) you have to be explicit.
Now obviously this gets really tedious if you have a value that you need to compare with a long list of values, for example:
if (x != 90 && x != 100 && x != 190...)
And in a case like that, it's usually easier to put all your values into an array and instead ask if x is contained (or not) in that array:
var values = [90,100,190,...]
if (!values.some(function(e) { return e === x; })) {
// the value x is not in the array values
}
For the &&, the first 100 != 90 evaluates to true. Then, the integers are implicitly converted to bools, where 0 = false, true otherwise. The true will carry through the numbers, of which none are 0.
Your comment about || is correct.

Reverse the logic of two "or" statements in JavaScript if query

I have an if test in JavaScript which achieves what I want but not as elegantly as possible. It goes like this:
if (x > y || p < q) {
// don't do anything
} else {
doSomeFunction();
}
If there any way to flip the logic of this so there's only a single if statement without having to have a dummy if-condition as well as the else condition?
You can use the ! operator to invert the condition:
if (!(x > y || p < q)) {
doSomeFunction();
}
Or simply rewrite the condition like this:
if (x <= y && p >= q) {
doSomeFunction();
}
Note: See De Morgan's laws for an explanation about why these two conditions are equivalent.
You can simply invert the comparisons and the logical OR.
if (x <= y && p >= q) {
doSomeFunction();
}
Along with the other answer, the last option (less readable) is :
(x > y || p < q) || doSomeFunction();
If the left bracket is true, it will NOT execute the function.

What is the best way to check >= and <= in Javascript?

I have a number.
I then have an if statement that I want to go like this:
if (5 <= variable <= 10)
So if the number is between 5 and 10, the statement should be true.
What's the best way to go about this?
Thanks.
it is
if (5 <= variable && variable <= 10)
if ((variable >= 5) && (variable <= 10)) works.
If you do this frequently, consider defining a bounds function:
function inBounds(value, min, max){
return ((value >= min) && (value <= max));
}
Actually, if ( 5>= variable >= 10 ) means if(false)
if you means variable between 5, 10 it should be 5 <= variable <=10, and in javascript, the best convention is const value at left, which is from c language if (v=1), if (1=v) problem. so the best is:
if (5 <= variable && 10 >= variable) {
It is my understanding that at the first conditional false, it stops the checking and moves to the next statement.
Given that, you should examine the most often false condition and put it first.
if ((variable >= 5) && (variable <= 10))
if it is more likely to be less than 4 or
if ((variable <= 10) && (variable >= 5))
if the failure of 10 or greater is more likely to occur.

Categories

Resources