Else block not fulfilling my condition - javascript

if(a == 1 || b == 0) {
document.getElementById("div").style.display = "none";
}
else {
document.getElementById("div").style.display = "block";
}
When I select a value 1 or b value 0 my div style set to none because condition is true, but when I select a value 2 or b value 3 means anything except 1 or 0 my div is not showing it's in display: none, it needs to display block.
Can you guyz know what I am doing wrong here?

Where do you get your a and b from?
Are you sure they are integers and not string?
You could try
if(parseInt(a) == 1 || parseInt(b) == 0) {

Related

If statement is acting weird for some reason

So I have an if statement that is checking the values of a drop down. The user has to select at least one of them to continue. The values are just numbers. My if statement checks to see that, basically, the values are equal to zero:
if (numChildren === 0 && numAdults === 0){
Than do this
}
But for some reason it keep returning false. I have console logged the numChildren and adults to see what there values are and they are zero I have no idea why it isn't working I have put a "!" before it to cause it to be true but then it remains false. It is a very weird error.
$("#Discounts").on("click", function () {
let numChildren = $("#children").val();
let numAdults = $("#adults").val();
console.log("Adults: " + numAdults + "\nChildren: " + numChildren);
console.log(numChildren === 0 && numAdults === 0);
if (numChildren === 0 && numAdults === 0) {
alert("You must select");
$("#children").val(1);
}
});
When you use 3 equals in a if statement the numChildren must be the same type and value as 0 (integer), not just equal to 0.
Try changing to 2 equals:
if (numChildren == 0 && numAdults == 0) {
alert("You must select");
$("#children").val(1);
}
Reference
It's not weird.
You are using the triple equal comparison and, probably, the numChildren contains a number in string form (like "0") which you are comparing to zero as an actual number.
It should work with:
numChildren == 0 && numAdults == 0
or
Number(numChildren) === 0 && Number(numAdults) === 0
This is the official reference: https://tc39.github.io/ecma262/#sec-strict-equality-comparison
You can try this:
if (Number(numChildren) === 0 && Number(numAdults) === 0) {
alert("You must select");
$("#children").val(1);
}

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.)

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);

A quick way to test equality of more than 2 values at once?

I was wondering if there was a quick way to test the equality of more than two values in js. Something similar to (= 6 6 6).
In the console, I tried things like...
1 == 1 == 1 == 1
true
2 == 2 == 2 == 2
false
0 == 0 == 0
false
0 == 0 == 0 == 0
true
...which was amusing, but also puzzling.
Is there a quick way of doing this in js?
Thanks.
The reason you got unexpected behavior is because we need to adjust your expectations in js a bit ;) 2 == 2 == 2 == 2 does 3 comparisons, all from left to right. The first comparison is the leftmost 2 == 2, which evaluates to true. After that we get the result of the first comparison being compared to (what is in this case) the 3rd 2. Ie, true === 2, which is false. And finally, we get false === 2, which is also false.
It might help to visualize it as such:
(((2 == 2) == 2) == 2)
I think in general a === b && b === c might be what you're looking for.
EDIT: Ah, and sorry I keep switching out the == for ===. It's just habit. And it's a habit I'd recommend. the === operator doesn't do type casting, so it evaluates the value proper, not a casted version of the value.
It's because true == 1 but true != 2
You can try:
function isEquals() {
var flag = true;
for(var i=1; i<arguments.length; i++) flag = flag && (arguments[i] == arguments[0]);
return flag;
}
isEquals(2,2,2); // true
or:
function isEquals() {
var ar = arguments;
return Array.prototype.every.call(arguments, function(a){return a==ar[0];});
}
Yes you can, but you need to use the "Logical Operators" like the && or || to check more than 1 statement like (x<1 && y>0).
You can use this as a quick easy reference:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators
If you have more than three values, it might be more convenient to create a function for use on an array:
function allEqual(arr) {
return arr.every(function (x, i) {
return i === 0 || x === arr[i - 1];
});
}
allEqual([1, 1, 1])
ES6:
function allEqual(...arr) {
return arr.every((x, i) => i === 0 || x === arr[i - 1]);
}
allEqual(1, 1, 1)
As an addition to #vp_arth's answer you could even add a method to the Array prototype
Array.prototype.isHomogeneous = function(){
return Array.prototype.every.call(this, function(c,i,a){ return c === a[0];})
}
So you could do
[1,2,3].isHomogeneous() = false
[1,1,1].isHomogeneous() = true

Categories