If variable matches 3 conditions - javascript

I need to have an if statement to check if a number is = 0, or between 5000 and 3600000. How can have it check all 3 conditions?
Number can be 0 or 5000 or above OR 3600000 or less. It can't be -1, 1.1 2.3 or anything between 0 and 5.
Here is what I have now and this work for checking if less than 5000 or greater than 3600000
var intervalCheck = interval * digit
if(isNaN(intervalCheck))
{
alert("Must be a number.");
}
else if((intervalCheck < 5000)||(intervalCheck>=3600000))
{
alert("Must be between 5 seconds and 1 hour.");
}
else
{
localStorage["interval_setting"] = interval * digit;
saveActions();
}

if ((intervalCheck == 0) || (intervalCheck >= 5000 && intervalCheck <= 3600000)) {
// put your code here
}
You can combine as many logic operations as you want into one if statement using any of the javascript logic and comparision operators.
You can see a demo work here: http://jsfiddle.net/jfriend00/y5tRK/

else if(intervalCheck === 0 || (intervalCheck >= 5000 && intervalCheck <= 3600000)) {
localStorage["interval_setting"] = interval * digit;
saveActions();
}
else {
alert('You broke something");
}
The === means to check both type and value. This makes sure that intervalCheck is both a number and equal to zero, instead of evaluating to true if intervalCheck is "" or false. Then we give our or condition. We wrap this in parenthesis becuase we need to ensure that the value falls between our two numbers. The parenthesis mean this will be evaluated as a whole with the or.

I need to have an if statement to check if a number is = 0, or between
5000 and 3600000.
if( intervalCheck == 0 || (intervalCheck >= 5000 && intervalCheck <= 3600000) ) {
//code here
}

if(intervalCheck == 0 || (intervalCheck >= 5000 && intervalCheck<=3600000))

if(!isNaN(intervalCheck) && (intervalCheck == 0 || (intervalCheck >= 5000 && intervalCheck <= 3600000)))

Related

JSHINT Throws Syntax Error for If statement equation

I am trying to check if the projectCount is divisible by 4 and not by 5 then vice versa in an else if statement. My code compiles however jshint throws 15 syntax errors. How else would I structure this to remove those syntax errors?
jQuery(document).ready(function($) {
if($(".projects li").hasClass("projectCols-30")) {
var projectCount = $('.projectCols-30').length;
if (projectCount % 4 = 0 && projectCount % 5 != 0) {
alert("The total number of columns means there is always only 1 extra on a row");
} else if (projectCount % 4 != 0 && projectCount % 5 = 0) {
alert("The total number of columns means there is always only 2 extra on a row");
}
}
});
Four warnings 5 Bad assignment. 5 Expected a conditional expression
and instead saw an assignment. 7 Bad assignment. 7 Expected a
conditional expression and instead saw an assignment.
After some edits, the following no longer throws syntax errors Talg123 please post this as an answer as you fixed the problem in a comment.
jQuery(document).ready(function($) {
if($(".projects li").hasClass("projectCols-30")) {
var projectCount = $('.projectCols-30').length;
if (projectCount % 4 === 0 && projectCount % 5 !== 0) {
alert("The total number of columns means there is always only 1 extra on a row");
} else if (projectCount % 4 !== 0 && projectCount % 5 === 0) {
alert("The total number of columns means there is always only 2 extra on a row");
}
}
});
Don't use a single = to compare numbers, this is the assignment operator.
Write projectCount % 4 == 0 or projectCount % 4 === 0 instead.
jQuery(document).ready(function($) {
if($(".projects li").hasClass("projectCols-30")) {
var projectCount = $('.projectCols-30').length;
if (projectCount % 4 === 0 && projectCount % 5 !== 0) {
alert("The total number of columns means there is always only 1 extra on a row");
} else if (projectCount % 4 !== 0 && projectCount % 5 === 0) {
alert("The total number of columns means there is always only 2 extra on a row");
}
}
});
well basicly you need to compare with which means that u need to use ==\=== for equal and not to set your variable with =

Fizzbuzz Example: What is the purpose of the equal-to operator in this example?

I know there are easier and quicker ways to write this program. However, I'm having trouble understanding why the equal-to operator is needed here? Referring to the == 0 instances below.
for(let x=1;x<101;x++) {
if(x % 3 == 0 && x % 5 == 0){
console.log('fizzbuzz')
} else if(x % 3 == 0) {
console.log('fizz')
} else if(x % 5 == 0) {
console.log('buzz')
} else {
console.log(x)
}
}
x % 3 == 0 is checking to see if x is evenly divisible by three. If it isn't, then there will be a non-zero remainder. (The x % 3 part of that expression uses the % operator to get the remainder after division.)

similar to FizzBuzz with a twist

Write a javascript program that displays the numbers from 10 to 100. But for multiples of 4 print "Penny" instead of the number and for multiples of 6 print "Leonard". For numbers which are multiples of both 4 and 6 print "Bazzinga"
I know how to do two parts struggling to print 6 and 4;
function baZzinga (number) {
for (var number = 10; number <= 101; number++)
if(number % 4 == 0) {
console.log("penny");
}
else if (number % 6 == 0) {
console.log("Leonard");
} else if ( not sure what goes here) {
help help help
} else {
console.log(number");
}
You want the and condition first. Try this
var result = document.getElementById("result");
function baZzinga (number) {
for (var number = 10; number <= 101; number++) {
if (number % 4 == 0 && number % 6 == 0) {
result.innerHTML += "Bazinga";
}
else if(number % 4 == 0) {
result.innerHTML += "penny";
}
else if (number % 6 == 0) {
result.innerHTML += "Leonard";
}
else {
result.innerHTML += number;
}
}
}
baZzinga()
<p id="result"></p>
I changed console.log to result.innerHTML because I wanted to demonstrate it in a snippet.
I have a few comments on your code -- constructive criticism, I hope! First, you don't need the number parameter in your bazzinga function. Next, the indentation of the code you posted makes it hard to read. Finally, you should almost always use === instead of ==. The === tests for strict equality, whereas == tries to do some type conversions first (and can therefore produce unexpected results). See the official docs.
To answer you question: check for divisibility by 6 AND 8 first. That way, it will override the individual cases. I believe you want something like this:
function bazzinga() {
for (var number = 10; number <= 100; number++) {
if (number % 4 === 0 && number % 6 === 0) {
console.log("Bazzinga");
} else if (number % 4 === 0) {
console.log("Penny");
} else if (number % 6 === 0) {
console.log("Leonard");
}
}
}
Here is a solution using the format you posted:
for (var number = 10; number <= 100; number++) {
if(number % 4 === 0 && number % 6 === 0){
console.log("bazzinga");
} else if(number % 4 === 0) {
console.log("penny");
} else if (number % 6 === 0) {
console.log("Leonard");
} else {
console.log(number);
}
}
Or use the ternary operator to be even more succinct!
for (var i = 10; i <= 100; i++){
var penny = i % 4 === 0;
var leonard = i % 6 === 0;
console.log(penny ? (leonard ? "bazzinga" : "penny"): leonard ? "leonard" : i);
}
function process_num(num) {
return num % 4 == 0 ? num % 6 == 0 ? "Bazzinga" : "Penny" : num % 6 == 0 ? "Leonard" : num;
}
for (x = 10; x <= 100; x++) { console.log( x + ': is ', process_num(x)) }
Nested Ternary operator for conciseness
If it passes outer ternary test it is divisible by 4:
Enter into nested termary one to test if num is also divisible by 6 for the BaZzinga prize!!
If it fails the BaZzinga challenge, we know it previously passed the divisible by 4 test so print "penny"
Failing the outer ternary condition, we know it's not divisible by 4:
Enter nested ternary two to consider if divisible by 6. If so print "Leonard".
If not it's failed both the outer (div by 4) and inner (div by 6) so return the number unchanged.
Now that the logic is contained in the function, we can just create a for loop to iterate over the required numbers printing out the correct values.

using an if statement inside a while loop

I just figured out how to test for certain conditions and modify output within a loop. But I noticed that testing for two conditionals with the && operator only works in an if/else if/else if/else chain if it's the first one tested for.
Can someone explain why this works:
var number = 0;
var counter = 0;
while (counter < 100) {
number ++;
counter ++;
if (number % 3 == 0 && number % 5 == 0)
console.log ("FizzBuzz");
else if (number % 3 == 0)
console.log("Fizz");
else if (number % 5 == 0)
console.log("Buzz");
else
console.log(number);
}
But this does not?:
var number = 0;
var counter = 0;
while (counter < 100) {
number ++;
counter ++;
if (number % 3 == 0)
console.log("Fizz");
else if (number % 5 == 0)
console.log("Buzz");
else if (number % 3 == 0 && number % 5 == 0)
console.log ("FizzBuzz");
else
console.log(number);
}
An else if, as the name suggests, will only execute when a previous if fails. So the statement else if (number % 3 == 0 && number % 5 == 0) will execute only when if (number % 3 == 0) and else if (number % 5 == 0) fail. If a number is a multiple of 3 and 5 both, then the first if gets successfully executed, and the rest ifs and else-ifs are ignored.
However, in code 1, the ordering of ifs and else-ifs is such that, if a number is divisible by both 3 & 5, then first if is executed, if it is divisible by the only 3, then first if is not executed, only else if (number % 3 == 0) is executed.
Let's make an example using the numbers 6, 10, 15.
The number 6 will execute - in your first example (the working example) - the second if block because in the first one the condition will not be satisfied while the third and fourth block will be ignored, and - in your second example (the not-working example) - will execute the first if block and ignore the other blocks that follow.
The number 10 will execute - in your first example - the third block because the first's and second's condition is not satisfied while the fourth block will be ignored, and - in your second example - will execute the second block, because the condition in the first block is not satisfied, while the blocks that follow will be ignored.
The number 15 will execute - in your first example - the first block and ignore the blocks that follow, and - in your second example - will also execute the first block because the condition is satisfied while the blocks that follow will be ignored.
So, to recap, in your second example, the third if block will never be executed because the condition for its execution is made up of an and of the first and second if block's conditions. In order for the third block to be executed you would need a case where the first if block's condition (let's say c1) and the second if block's condition (let's say c2) are false and c1 && c2 is true, but in order to have c1 && c2 to true you need c1 and c2 to be true, which leads to the execution of the first block and skipping of the rest.
You want to test for if the the number is divisible by three and five, but before you do that you test if it is just divisible by three.
It is, so it follows that branch of logic and never attempts to test if it is divisible by three and five.
Because in your test if the number is a multiple of 3 or 5 then the corresponding if statemetn will get executed before the number % 3 == 0 && number % 5 == 0 statement is reached so it will never get executed.
Let us assume the number is 33, the the first test will become success which is correct, but if the number if 15 then again the first if is success because 15 is a multiple of 3 so even though it is a multiple of 5 also the 3rd condition will not get a chance to execute
To get it correct you may need something like below, where if the number is a multiple of both the versions we skip first 2 conditions
var number = 0;
var counter = 0;
while (counter < 100) {
number++;
counter++;
if (number % 3 == 0 && number % 5 != 0) {
console.log("Fizz");
} else if (number % 5 == 0 && number % 3 != 0) {
console.log("Buzz");
} else if (number % 3 == 0 && number % 5 == 0) {
console.log("FizzBuzz");
} else {
console.log(number);
}
}
Everything that is either evenly divisible by 3 or evenly divisible by 5 has been removed in the second version. By the time it checks to see if a number is divisible by 3 and divisible by 5 there is no chance of it being true because one of the first two clauses already evaluated to be true.
Consider this pseudo code
if( A || B ) return;
if( A && B ) //this code will never execute
and then consider A to be number % 3 == 0 and B to be number % 5 == 0. This is essentially what is happening, and why the last if statement never executes.
What you actually want to test is
if (number % 3 == 0 && number % 5 == 0) …
else if (number % 3 == 0 && number % 5 != 0) …
else if (number % 3 != 0 && number % 5 == 0) …
else if (number % 3 != 0 && number % 5 != 0) …
if you'd write out the four cases.
Only you don't need to be that explicit, because when the previous conditions already did not match (and you are in the else branch), then those != 0 are implied and you can omit them. However, order matters, as the conditions are tested consecutively.
So if you have the fully qualified conditions, you can shuffle their order as you want:
if (number % 3 == 0 && number % 5 != 0) … // move to front
else if (number % 3 != 0 && number % 5 == 0) …
else if (number % 3 == 0 && number % 5 == 0) …
else if (number % 3 != 0 && number % 5 != 0) …
and then continue to simplify conditions, omitting the parts that are already implied by their parent cases:
if (number % 3 == 0 && number % 5 != 0)
console.log("Fizz");
else if (number % 3 != 0 && number % 5 == 0) // (an == instead of the && would suffice)
console.log("Buzz");
else if (number % 3 == 0) // as it didn't match the first condition, we know that % 5 == 0
console.log("FizzBuzz");
else // here we know that % 3 != 0 && % 5 != 0
console.log(numer);
Other permutations of the condition let us use as few as in your original example, like
if (number % 3 == 0 && number % 5 != 0)
console.log("Fizz");
else if (number % 3 == 0) // as it didn't match the first condition, we know that % 5 == 0
console.log("FizzBuzz");
else if (number % 5 == 0) // as it didn't match the first condition, we know that % 3 != 0
console.log("Buzz");
else // here we know that % 3 != 0 && % 5 != 0
console.log(numer);
And the minimum number of tests would be achievable by nesting them:
if (number % 3 == 0)
if (number % 5 == 0)
console.log("FizzBuzz");
else
console.log("Fizz");
else
if (number % 5 == 0)
console.log("Buzz");
else
console.log(numer);

Check if value is multiple of 10

I want to check if a value is a multiple of a certain number, for example, multiples of 10, but I also want to be able to change it to whatever I want.
if (directWinner == 10){
}
You'd use the modulus operator for that :
if (directWinner % 10 === 0){
directWinner = 20;
}
Added a small dose of jQuery for no good reason at all ?
$.modu = function(check, against) {
return check % against === 0;
}
if ( $.modu(directWinner, 10) ) {
directWinner = 20;
}
You'd use the modulo operator % for that:
var certainNumber = 10;
if (directWinner % certainNumber === 0) {
// directWinner is a multiple of certainNumber
}
Use the modulo operator (assuming positive integers) :
if (directWinner % 10 === 0) {
...
}

Categories