Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make a variable be a condition?

I have been trying to make a variable have two values/be a condition. It’s fine if this is not possible, but I would like to not have to type 68 || 39 every time I would like to check. I have tried var left = 68 || 39 and var left = 68 && 39, but neither of those work. The variable is to check an event.keyCode number. I am trying to make my first game-in-javascript. Is this possible and if so how?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I don’t believe there is a way to make a variable be a condition but what you can do is define a method that checks if the value is 69 or 39.

For example:

function valueIs69or39(valueToCheck) {
    return valueToCheck == 69 || valueToCheck == 39;
}

now you can just use if (valueIs69or39(value)) to check, and you can shorten the method name to make it easier to type.

Hope this helps!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading