Multiple and different conditions in Javascript - javascript

I have a problem with logic.
If the number is higher than 0 and less than 10 AND the number is 11 AND the number is 22 do something. (1, 2, 3, 4, 5, 6, 7, 8, 9, 11 or 22).
And ELSE IF the number is higher than 10 BUT is NOT 11 or is NOT 22, do something else. (10, 12, 13 ....20, 21, 23, 24...)
I got to do only with 11, but I have no clue how to insert another condition with 22.
if (n >= 0 && n < 10) || (n == 11 && n == 22) {
do something
} else if (n >= 10 && n != 11) && (n != 22) {
do something else
}

Try (n >= 1 && n <= 9) || n == 11 || n == 22 which will check if the number is between 1 and 9 inclusive (1 to 9) OR it's 11 OR it's 22.
Then, in your else if, just check for >= 10 - numbers less than 1 e.g. 0, won't trigger the else either.
if ((n >=1 0 && n <= 9) || n == 11 || n == 22) {
do something
} else if (n >= 10) {
do something else
}
Use >= and <= to make it clearer what values you're looking for otherwise it takes a little extra mental load to realise that n > 0 && n < 10 really means values 1 to 9.
Also, it's JavaScript and numbers can either be integral or floating point, so greater than zero could be 0.1 etc.

In your if statement, you say:
n is larger or equal to 0 AND n is smaller than 10
OR
n is equal to 11 AND n is equal to 22
Which means, the if block will run for numbers that are between 0 and 9 (inclusive) OR for the numbers 11 AND 22. So the number can either be between 0 and 9 or it can be BOTH 11 and 22, which is impossible.
My take on the if statement:
if (n >= 1 && n <= 9 || n == 11 || n == 22) {
do something;
}
That way, you don't get confused with the inclusive and exclusive numbering and make sure that either 11 or 22 is allowed.
The second block will run for numbers that are greater or equal to 10 AND not 11 AND not 22. Which is just fine.
else if (n >= 10 && n != 11 && n != 22) {
do something else;
}
In this answer, I assume you don't want to entirely skip the 10. If you do, then in the second statement, you would check for n > 10.
Logically, this is sound. However, I don't think that you need to check for 11 and 22 in the second one, as the if block will run first and if the number is 11 or 22, the first if block will run. So the second one doesn't need to account for them.

Based on your example of (1, 2, 3, 4, 5, 6, 7, 8, 9, 11 or 22) it sounds like this is what you're going for?
if ((n > 0 && n < 10) || n == 11 || n == 22) {
// do something
} else if (n >= 10) {
// do something else
}
Edit: Added else if (n >= 10) that I overlooked

Just take the comparison and because of the higher precedence of logical AND && over logical OR ||, you need no parenthesis.
if (n >= 0 && n < 10 || n === 11 || n === 22) {
// 1, 2, 3, 4, 5, 6, 7, 8, 9, 11 or 22
// do something
} else {
// 10, 12, 13 ... 21, 23 ...
// do something
}

Your first condition will never succeed because you can't have a number higher than 0 and less than 10 AND this same number is equal to 11 AND also being equal to 22 (in both case, it'll be more than 10).
if ((n > 0 && n < 10) || n == 11 || n == 22) {
// my number is 1, 2, 3, 4, 5, 6, 7, 8, 9, 11 or 22
} else {
// my number is almost everything except the values from above
}

You were almost there.
To test for 1, 2, 3, 4, 5, 6, 7, 8, 9, 11 or 22:
if (n >= 0 && n < 10) || (n == 11 && n == 22) {
Can minimally be changed to:
if ((n > 0 && n < 10) || (n == 11 || n == 22)) {
The three important changes are:
balance parentheses to make a proper if( ) construct.
>= 0 becomes > 0 to achieve the higher than zero requirement.
The last && becomes ||.
This also changes the order of operations such that we don't actually need any extra parentheses at all in this case. This also happens to be correct, and is simpler:
if (n > 0 && n < 10 || n == 11 || n == 22) {
...because && has higher precedence than || and the comparators (>, < and ==) have higher precedence than the conditionals (&& and ||).
At this point you don't need an else if condition. Just else will suffice. 11 and 22 are handled by the affirmative case of the if and therefore n will be neither of those things in the else case.
Final product:
if (n > 0 && n < 10 || n == 11 || n == 22) {
// 1, 2, 3, 4, 5, 6, 7, 8, 9, 11 or 22
} else {
// Something else. :)
}

Take a look at the documentation, don't confuse logicial AND (&&) and logical OR (||).
function check(n) {
if((n >= 1 && n <= 9) || (n == 11 || n == 22)) {
console.log(n, true);
} else if(n >= 10) {
console.log(n, false);
}
}
check(1) // true
check(11) // true
check(22) // true
check(0)
check(10) // false
check(12) // false
check(21) // false
check(23) // false

Related

A program that prompts the user to enter a number, and then checks whether the number is divisible by 7 and 11, or either 7 or 11

//TASK 5 PART 1
//Request input from user
let x = Number(prompt("Enter a number:"));
// using if...else if
if (x % 7 === 0 &&
x % 11 === 0) {
console.log
(x + " is divisible by 7 and 11");
}
else if (x % 7 === 0 ||
x % 11 === 0) {
console.log
(x + " is divisible by either 7 or 11");
}
else {
console.log
(x + " is divisible by neither 7 or 11");
}
I am very new to JavaScript, and so I am here to ask for help on how to make my code more efficient, using less lines of code, perhaps a method better suited for this program, improve readability or if anybody spots any errors. I am always keen on receiving constructive criticism on where to improve.
Thank you all for your time.
(x % 7 == 0) && (x % 11 == 0) ? "both" : (x % 7 == 0) || (x % 11 == 0) ? "either" : "neither"
Here I have used the nested ternary operator, to do the solution in one line.
The syntax is:
let result = condition ? value1 : value2;
The condition is evaluated: if it’s truthy then value1 is returned, otherwise – value2.
To know more please refer to this article.
In my expierince first get comfortable with the basics first, then look to enhance your knowledge of advanced concept of Javascript.
I'd write it like this.
const x = Number(prompt('Number:'));
const d7 = !(x % 7), d11 = !(x % 11);
const result = (d7 && d11) ? 'both 7 and 11' :
(d7 || d11) ? 'either 7 or 11' :
'neither 7 nor 11';
console.log(`${x} is divisible by ${result}`);

Check whether three given integer values are in the range 50..99

I want to create a function to check 3 values.
If the 3 values are in the range 50..99, I return true, otherwise false.
In this example, normally the answer is false.
console.log(check_three_nums(65, 9, 199));
I have as answer true.
I don't understand, why?
function check_three_nums(x, y, z) {
if ((x >= 50 && x <= 99) || (y >= 50 && y <= 99) || (z >= 50 && z <= 99)) {
return true;
} else {
return false;
}
}
// console.log(check_three_nums(50, 90, 99));
// console.log(check_three_nums(5, 9, 199));
// console.log(check_three_nums(65, 89, 199));
console.log(check_three_nums(65, 9, 199));
You can use this function to check any amount of number.
function check(...args) {
for (let a of args) {
if (a < 50 || a > 99)
return false
}
return true
}
The double pipe operator (||) means OR, your function is currently returning true if at least one of the param is in your range.
Try to change it whith the '&&' operator
It should work as expected
function check_three_nums(x, y, z) {
if ((x >= 50 && x <= 99) && (y >= 50 && y <= 99) && (z >= 50 && z <= 99)) {
return true;
} else {
return false;
}
}
// console.log(check_three_nums(50, 90, 99));
// console.log(check_three_nums(5, 9, 199));
// console.log(check_three_nums(65, 89, 199));
console.log(check_three_nums(65, 9, 199));
"or operator" (||) will output true if one of the parameter values ​​is true (condition1 is true or condition2 is true). You can use the "and operator" (&&), this operator will only output true if both parameter values ​​are true (condition1 and condition2 are true)

How to print the right name in the code below?

for (var i = 1; i <= 50; i++) {
if (i % 15 === 0 || i % 10 === 0) {
console.log("Donkey!");
} else if (i % 2 !== 0 && (i - 1) % 10 === 0) {
console.log("Monkey!");
} else if (i % 7 === 0) {
continue;
} else {
console.log(i);
}
}
emphasized textIf the number is not divisible by 2 and the previous number is divisible by 10, How do I print Monkey!?
This a very random question with no context but has a rather simple solution. Use modulus check if the remainder of the current number divided by 2 is not 0 and check if the remainder of the last number divided by 10 is 0:
function check(n) {
if (n % 2 !== 0 && (n - 1) % 10 === 0) {
console.log('Monkey!');
}
}
check(11)

Time based javascript, creating a blank response for approx 30 minutes

First - thanks in advance for your help.
I have a image based slide show, that calls #photo1, 2, 3. Using javascript to change the image (actually the image folder, all images have the same name, so just changing root folder) based on various time blocks throughout the day. (9am-11am, 11am-3pm, 3-6pm, 6pm-9am). In case it matters, I am storing the images on dropbox (so my buddy can change them at will, the links provided are old and not currently working).
It all seems to be working, accept for approx the first 30 minutes after the change time, it goes blank. Then works fine again.
Thoughts? I am not great with javascript, so if I am going about this all wrong, I am open to new approaches.
Javascript code:
$(document).ready(function(){
var d = new Date();
var n = d.getHours();
// If time is after 9AM or before 11AM
if (n > 9 && n < 11) {
$("img#photo1").attr("src","https://dl.dropboxusercontent.com/s/324....67l/ad1.jpg?dl=0");
$("img#photo2").attr("src","https://dl.dropboxusercontent.com/s/5i9f....m10z/ad2.jpg?dl=0");
$("img#photo3").attr("src","https://dl.dropboxusercontent.com/s/n7....66/ad3.jpg?dl=0");
}
else if (n > 11 && n < 15) {
$("img#photo1").attr("src","https://dl.dropboxusercontent.com/s/31p....rw3r/ad1.jpg?dl=0");
$("img#photo2").attr("src","https://dl.dropboxusercontent.com/s/h3m....tsqy/ad2.jpg?dl=0");
$("img#photo3").attr("src","https://dl.dropboxusercontent.com/s/zr9....kzja/ad3.jpg?dl=0");
}
else if (n > 15 && n < 18) {
$("img#photo1").attr("src","https://dl.dropboxusercontent.com/s/5li....y2w/ad1.jpg?dl=0");
$("img#photo2").attr("src","https://dl.dropboxusercontent.com/s/no....twr2/ad2.jpg?dl=0");
$("img#photo3").attr("src","https://dl.dropboxusercontent.com/s/ojjn....i1g/ad3.jpg?dl=0");
}
else if (n > 18 || n < 9) {
$("img#photo1").attr("src","https://dl.dropboxusercontent.com/s/kn3ch....s7g0/ad1.jpg?dl=0");
$("img#photo2").attr("src","https://dl.dropboxusercontent.com/s/dhxi5....04/ad2.jpg?dl=0");
$("img#photo3").attr("src","https://dl.dropboxusercontent.com/s/mb51....336/ad3.jpg?dl=0");
}
// runs the code every hour, probably there is a much better way to do this
setTimeout(function(){window.location.reload();}, 3600000)
});
When the hour is 9, 11, 15 or 18, none of your conditions will match, and nothing will be displayed. Change the conditions to:
if (n >= 9 && n < 11) {
// ...
}
else if (n >= 11 && n < 15) {
// ...
}
else if (n >= 15 && n < 18) {
// ...
}
else if (n >= 18 || n < 9) {
// ...
}
You've excluded 9, 11, 15 and 18; the if statements selects for greater than or less than. You'll get undefined for any of those numbers.

JavaScript if statement test for one or the other but not both

Hello fellow StackOverflowers. I'm have a brain fart right now, and I cannot seem to figure this out.
I have the following code
if ((n % 3 === 0 || n % 5 === 0) &&( n % 3 !== 0 && n % 5 !== 0))
{
return true;
}
else {
return false;
}
Basically I need to test if the number is a multiple of 3 || 5 but not a multiple of both.
However when I enter any number I enter (whether it is multiple of 3 || 5 || both) the test always fails. I would have thought this was able to be performed in one statement.
This code though does work fine.
if (n % 3 === 0 || n % 5 === 0)
{
if( n % 3 === 0 && n % 5 === 0)
{
return false;
}
else {
return true;
}
}
else {
return false;
}
But I'm wondering what I am missing in the first test. I'd like all the test to be in one like, but like I said I'm having a brain fart and cannot figure out what I'm missing.
You can use the XOR operator, alternatively
return (n % 3 === 0 ^ n % 5 === 0);
If it is divisible by both 3 and 5, it'll be divisible by 15.
Please try the following condition
if ((n % 3 === 0 || n % 5 === 0) && ( n % 15 !== 0))
change
if ((n % 3 === 0 || n % 5 === 0) &&( n % 3 !== 0 && n % 5 !== 0))
to
if ((n % 3 === 0 || n % 5 === 0) && !(n % 3 === 0 && n % 5 === 0))
The first part of your logic is to determine if the number in question is a multiple of 3 or 5 whereas the second SHOULD be about wether only one of them is. So... I changed the second part to see if both match it and then I NOT'ed that.
It should be: if ((n % 3 === 0 || n % 5 === 0) &&( n % 3 !== 0 || n % 5 !== 0))
return (n % 3 === 0 && !(n % 5 === 0)) || (n % 5 === 0 && !(n % 3 === 0));
(untested)
Your second check is wrong:
if ((n % 3 === 0 || n % 5 === 0) &&**( n % 3 !== 0 && n % 5 !== 0)**)
Change it to:
(! (n%3 === 0 && n % 5 === 0 ) )
This is a short version of XOR implementation using conditional statement in javascript
if((n % 3 === 0)? (n % 5 !== 0) : (n % 5 === 0)) {
...
}
or you can also compare in this way, checking when the two conditions, when evaluated as boolean, return different values (one is true and other is false or vice-versa)
if( (n % 3 === 0) !== (n % 5 === 0)) {
...
}
so this code can be written really short

Categories

Resources