JavaScript If Statement with Multiple Permissible Conditions

Javascript if statement with multiple permissible conditions

You could use an array like this:

var extensions = ["jpg", "png", "gif"];

if (extensions.indexOf(aExtensions[i].toLowerCase()) > -1) {
// match
}

In this case, you store the "valid" extensions. Then, you can use the indexOf method of Array to find if any of the items in the array match the specific extension you're looking at - checking for a value that is 0 or greater.

indexOf isn't supported on older browsers, so you'd need to include a polyfill to back it up. There are several solutions for that.

Of course, if you wanted to only use if statements, you could use this format:

var ext = aExtensions[i].toLowerCase();
if (ext == "jpg" || ext == "png" || ext == "gif") {
// match
}

Another possibility I can think of is a switch statement, like:

var ext = aExtensions[i].toLowerCase();

switch (ext) {
case "jpg":
case "png":
case "gif":
// match
break;
case default:
// No match
}

I might as well include it (other answers had it first, definitely), but one more I can think of is using a regex, like:

if (/jpg|png|gif/i.test(aExtensions[i])) {
// match
}

But note that you will never be able to individually get the extensions back, and that's why I would prefer one of the first two options I gave.

java script if condition with multiple values

Your if condition is wrong, you are suppose to compare role variable with the role you want to check. As @lokesh mentioned in his code.

(role === "admin" || role === "manager" || role === "staff")

Another way of doing this:

let role = "restaurant";    if(["admin", "manager", "staff"].includes(role)){  console.log("IF PART")  console.log(role)}else{  console.log("ELSE PART")}

javascript multiple OR conditions in IF statement

With an OR (||) operation, if any one of the conditions are true, the result is true.

I think you want an AND (&&) operation here.

How do i add multiple conditions in an if statement in javascript?

I made one condition for you take a look. My condition matched when you select "outdoors" from place, "Sunny" from weather and "casual" from look. If you have multiple conditions then you can else if after if condition

<h1>Clothes Picker</h1>
<form action="clothespicker.html" id="clothespicker">
<fieldset>
<label for="place">Where are you going today?</label>
<div>
<select name="place" id="place">
<option value="Someone's House">Someone's House</option>
<option value="Outdoors">Outdoors</option>
<option value="Shopping">Shopping</option>
<option value="Local Shopping">Local Shopping</option>
<option value="Somewhere Special">Somewhere Special</option>
</select>
</div>

<label for="weather">What's the weather?</label>
<div>
<select name="weather" id="weather">
<option value="Sunny" id="Sunny">Sunny</option>
<option value="Cold">Cold</option>
<option value="Rainy">Rainy</option>
<option value="Cloudy">Cloudy</option>
</select>
</div>

<label for="look">How do you want to look?</label>
<div>
<select name="look" id="look">
<option value="Casual" id="Casual">Casual</option>
<option value="Formal">Formal</option>
<option value="I don't know">I don't know</option>
</select>
</div>
<input type="submit" onclick="myFunction()" value="Pick My Clothes!" id="button">
</fieldset>
<p id="demo"></p>
</form>
<script>
function myFunction() {
var place = document.getElementById("place").value;
var weather = document.getElementById("weather").value;
var look = document.getElementById("look").value;
if(place == "Outdoors" && weather == "Sunny" && look == "Casual")
{
//your condition here
document.getElementById("demo").innerHTML = "Condition Matched";
}
}
</script>

Using multiple conditions in ? : statements in javascript

Yes, you can use convert it to Conditional(ternary) operator.

var a = "z3";var b = a == "z1" ? "one" : a == "z2" ? "two" : a == "z3" ? "three" : "" ;
console.log(b);

multiple arguments and conditions in a function in javascript

I resolve that problem by just checking the arguments in the main function:

function getEmployeesCoverage({ name, id } = {}) {
if (name) return getEmployeeByName(name);
if (id) return getEmployeeById(id);
if (!name || !id) return getAllEmployees();
}

And then calling the unique job function that verifies if that argument exist in database and if not throw and error.

function getEmployeeByName(name) {
if (employees.find((employee) => employee.lastName === name)
|| employees.find((employee) => employee.firstName === name)) {
const {
id: employeeId,
firstName,
lastName,
responsibleFor,
} = employees.find((employee) => employee.lastName === name)
|| employees.find((employee) => employee.firstName === name);
return {
id: employeeId,
fullName: `${firstName} ${lastName}`,
species: getSpeciesByIds(...responsibleFor).map((animal) => animal.name),
locations: getSpeciesByIds(...responsibleFor).map((animal) => animal.location),
};
}
throw new Error('Invalid Informations');
}

Javascript: Comparing SINGLE Value Against MULTIPLE Values with OR Operands

You could use a variable and multiple comparison, but that's still lengthy:

var text = item[i].innerText;
if (text === 'Argentina' || text === 'Australia' || text === 'Brazil' || text === 'Canada' || text === 'China' || text === 'Colombia' || text === 'France' || text === 'Germany' || text === 'Indonesia' || text === 'India' || text === 'Italy' || text === 'Japan' || text === 'Malaysia' || text === 'Mexico' || text === 'Philippines' || text === 'Russia' || text === 'South Africa' || text === 'Sweden' || text === 'Switzerland' || text === 'United Kingdom' || text === 'USA')

Or you could just use an array and check if the string is contained in it.

var matches = ['Argentina','Australia','Brazil','Canada','China','Colombia','France','Germany','Indonesia','India','Italy','Japan','Malaysia','Mexico','Philippines','Russia','South Africa','Sweden','Switzerland','United Kingdom','USA'];
if (~ matches.indexOf(item[i].innerText) …

Yet, for the complicated != -1 comparison and the lack of native indexOf in older IEs, people tend to use regexes:

var regex = /Argentina|Australia|Brazil|Canada|China|Colombia|France|Germany|Indonesia|India|Italy|Japan|Malaysia|Mexico|Philippines|Russia|South Africa|Sweden|Switzerland|United Kingdom|USA/
if (regex.test(item[i].innerText)) …

How to reduce multiple OR conditions in if statement in JavaScript?

You can also use the newer Array.includes

if (['a','b','c','d','e'].includes(item)) {
...
}

Another option (for the very specific case you posted) would be to compare unicode point values using </>

if (item >= 'a' && item <= 'e') {
...
}

If statement multiple conditionals on one value without repeating value

the only way to simplify this I know is to create separate functions with meaningful names, like:

function inRange(value, min, max) {
return (value > min) && (value < max);
}
function outRange(value, min, max) {
return (value < min) || (value > max);
}

then your condition will look like this:

if (outRange(keyCode, 48, 102) || inRange(keyCode, 57, 65) || inRange(keyCode, 70, 97) ) {
...
}

which is a little bit easier to understand because you see original intent, instead different > < and trying to understand what it actually means

another approach for this could be creation of the object rangeChecker/notInRangeChecker, then create array of different range checkers and create function checkValueAtLeastInOneRange(keyCode, rangesArray)



Related Topics



Leave a reply



Submit