Logical Operators in Javascript - javascript

I want to print out FizzBuzz when i is both divisible by 3 and by 5. What could be the problem with my code?
for(var i = 1; i<=20; i++){
if(i % 3 ===0){
console.log("Fizz");
}else if(i % 5 ===0){
console.log("Buzz");
}else if(i%3 ==0 && i%5 ==0){
console.log("FizzBuzz");
}else{
console.log(i);
}
}

If the first or second condition is true, it enters that block, but doesn't evaluate any of the other else if conditions. Because the third condition requires both the first and second to be true, there's no way it will ever enter that block.
Try arranging your conditions like this:
for(var i = 1; i<=20; i++){
if(i%3 === 0 && i%5 === 0){
console.log("FizzBuzz");
}else if(i % 3 === 0){
console.log("Fizz");
}else if(i % 5 === 0){
console.log("Buzz");
}else{
console.log(i);
}
}
But just for fun, here's a much more compact version that abuses the conditional operator:
for(var i = 1; i<=20; i++){
console.log(i % 15 ? i % 5 ? i % 3 ? i : "Fizz" : "Buzz" : "FizzBuzz");
}

The main issue is that your check for "FizzBuzz" doesn't happen until after your other comparisons. If i % 3 === 0 (one of the requirements to print "FizzBuzz"), it will never reach the FizzBuzz check.
As a simple fix, move your FizzBuzz check to the first if-statement.
for(var i = 1; i <= 20; i++) {
if(i % 3 === 0 && i % 5 === 0) {
console.log("FizzBuzz");
}
else if(i % 5 === 0) {
console.log("Buzz");
}
else if(i % 3 === 0) {
console.log("FizzBuzz");
}
else {
console.log(i);
}
}
As another thing to think about, if i is divisible by both 3 and 5, then it is divisible by their least-common denominator, yes? The least common denominator (the smallest whole number that is divisible by a group of numbers) of 3 and 5 is 15, so you could replace...
if(i % 3 === 0 && i % 5 === 0) {
...with...
if(i % 15 === 0) {

(i%3 ==0 && i%5 ==0) should be the first condition. If you think about it, if i is divisible by 3 and by 5 it will enter the first if statement before it reaches the third.

Related

javascript for loop: issues

i came across this problem: Write a program that uses console.log to print all the numbers from 1 to 100, with two exceptions. For numbers divisible by 3, print “Fizz” instead of the number, and for numbers divisible by 5 (and not 3), print “Buzz” instead. When you have that working, modify your program to print “FizzBuzz” for numbers that are divisible by both 3 and 5 (and still print “Fizz” or “Buzz” for numbers divisible by only one of those).
and i tried solving it with the code below:
for(let i = 1; i <= 100; i++){
if(i % 3 ===0) {
console.log("fizz");
} else if ( i % 5 === 0 ) {
console.log("buzz");
} else if (i % 5 === 0 && i % 3 === 0) {
console.log("fizzbuzz");
}
console.log(i);
}
please can anyone tell me what i did wrong because i am not getting result
In your condition, i = 15 should be returned fizzbuzz but it returns fizz because 15 can be divided by 3 and 5 so you first condition i % 3 === 0 getting true so it returned fizz. if your first condition is i % 3 === 0 && i % 5 === 0 then i = 15 should be return fizzbuzz.
for(let i = 1; i <= 100; i++){
if (i % 5 === 0 && i % 3 === 0) {
console.log("fizzbuzz");
} else if(i % 3 ===0) {
console.log("fizz");
} else if ( i % 5 === 0 ) {
console.log("buzz");
}
console.log(i);
}

How to join modulo javascript [Native]

here is my js
while(i <= 25){
if (i % 4 === 0) {
text += 'john';
}
else if (i % 5 === 0) {
text += 'doe'
}
else if ((i % 5 === 0)&&(i % 4 === 0)){do something }
Multiples are both 4 and 5 print 'john doe'
I need to join modulo 5 and 4. Anybody help?
Thank you
while(i <= 25){
if ((i % 5 === 0)&&(i % 4 === 0)) {
do something
}
else if (i % 5 === 0) {
do something
}
else if (i % 4 === 0){ do something }
I hope it will help you.

How to print a value with the same criteria - JavaScript

I am trying to print numbers that are divisible by 3 and 5.
For numbers divisible by 3, print out "Fizz".
For numbers divisible by 5, print out "Buzz".
For numbers divisible by both 3 and 5, print out "FizzBuzz" in
the console. Otherwise, just print out the number.
Tried these codes:
var i;
for(i = 1; i <= 20; i++){
if(i % 3 === 0){
console.log("Fizz");
}else if(i % 5 === 0){
console.log("Buzz");
}else if(i % 3 === 0 && i % 5 === 0){
console.log("FizzBuzz");
}else {
console.log(i);
}
}
But it seems it passing out the 3rd else if...
Is there a better way to do this?
You could move the combined comparsion to top of the comparisons, because if both conditions are true, you need not to check the others.
var i;
for (i = 1; i <= 20; i++) {
if (i % 3 === 0 && i % 5 === 0) { // check first, includes
console.log("FizzBuzz");
} else if (i % 3 === 0) { // this comparison and
console.log("Fizz");
} else if (i % 5 === 0) { // this as well.
console.log("Buzz");
} else {
console.log(i);
}
}
Less code for same results:
for(var i = 1; i <= 20; i++) {
var output = '';
if(i % 3 === 0) output += 'Fizz';
if(i % 5 === 0) output += 'Buzz';
console.log(output.length > 0 ? output : i);
}
A number divisible by two numbers must be divisible by their Least common multiple, which in this case is 15. You also have to move this check up, so that it's considered before the other statements.
for (var i = 1; i <= 20; i++) {
if (i % 15 == 0)
console.log('FizzBuzz');
else if (i % 3 === 0)
console.log("Fizz");
else if (i % 5 === 0)
console.log("Buzz");
else
console.log(i);
}
var i;
for (i = 1; i <= 20; i++) {
if (i % 3 === 0 && i % 5 === 0) {
document.write("FizzBuzz");
} else if (i % 3 === 0) {
document.write("Fizz");
} else if (i % 5 === 0) {
document.write("Buzz");
} else {
document.write(i);
}
}

Problems with Modulo in JavaScript

Doing some practice runs on codecademy and came across the following problem: So the objective is to print "Fizz" if the numbers are divisible by 3. "Buzz" if the numbers are divisible by 5. And "FizzBuzz" if the numbers are divisible by both 3 and 5.
Here is my code, and I thought I had it right, but when I run it they tell me that my code is not 100% accurate. Looking to see any alternatives to this code, or what might be the issue...
Code:
for ( i = 0; i < 21; i++)
{
if (i % 3 == 0 )
{
console.log("Fizz");
}
if (i % 5 == 0)
{
console.log ("Buzz");
}
if ( i % 5 == 0 && i % 3 === 0)
{
console.log("FizzBuzz");
}
else
{
console.log(i);
}
}
You need to use else if to stop from other conditions executing:
for (var i = 1; i <= 20; i++) {
if (i % 5 === 0 && i % 3 === 0) {
console.log("FizzBuzz");
} else if (i % 3 === 0) {
console.log("Fizz");
} else if (i % 5 === 0) {
console.log("Buzz");
} else {
console.log(i);
}
}
It can easily be done in a one liner.
for (i = 1; i <= 20; i++) {
console.log(i%3?(i%5?i:'buzz'):(i%5?'fizz':'fizzbuzz'));
};
For a nice formatting also output i on each iteration:
for (i = 1; i < 21; ++i) {
console.log(i+": "+(i%3?(i%5?i:'buzz'):(i%5?'fizz':'fizzbuzz')));
};

checking two different numbers modulos to 0 at the same time(Javascript)

I'm trying to get a program to print out a console log statement if a number 1 - 20 is divisible by 3, 5, or both. This is what I'm using, but it wont check both numbers....
for (i = 1; i < 21 ; i++) {
if (i % 3 === 0) {
console.log("Fizz");
} else if (i % 5 === 0) {
console.log("Buzz");
} else if ((i % 5)&&(i % 3) === 0) {
console.log("FizzBuzz");
} else {
console.log(i);
}
}
I tried searching for this, but I think my search criteria was not well stated.
Thank you guys!
You've got a typo in the third comparison. It should be this:
else if ((i % 5) === 0 &&(i % 3) === 0)
Also, that comparison has to be first, otherwise it gets short-circuited by the other two.
if (i % 5 === 0 && i % 3 === 0) {
console.log("FizzBuzz");
} else if (i % 3 === 0) {
console.log("Fizz");
} // ...
Fiddle
first check both
for (i = 1; i < 21 ; i++) {
if (i % 5 == 0 && i % 3 == 0) {
console.log("FizzBuzz");
}else if (i % 3 === 0) {
console.log("Fizz");
} else if (i % 5 === 0) {
console.log("Buzz");
}else {
console.log(i);
}
}
for (i = 1; i < 21 ; i++) {
if ((i % 5)==0 && (i % 3) == 0) {
console.log("Fizz");
} else if (i % 5 === 0) {
console.log("Buzz");
} else if (i % 3 === 0) {
console.log("FizzBuzz");
} else {
console.log(i);
}
The one that sees if both are applicable needs to be first, otherwise it will never be met.
And you need to evaluate that condition as: (i % 5) == 0 && (i % 3) == 0
There are two issues:
Checking (x && y) === 0 is not the same as checking (x === 0) && (y === 0)
You have to put the third condition first, otherwise it will never be reached
Code:
for (i = 1; i < 21 ; i++) {
if ((i % 5 === 0) && (i % 3 === 0)) {
console.log("FizzBuzz");
} else if (i % 3 === 0) {
console.log("Fizz");
} else if (i % 5 === 0) {
console.log("Buzz");
} else {
console.log(i);
}
}

Categories

Resources