FizzBuzz: num%15 being first versus num%3 being first - javascript

I just want to know why 'FizzBuzz only works when num % 15 === 0 is first as opposed to when num % 3 === 0 is first.
For example:
for (let num = 1; num <= 100; num++) {
if (num % 15 === 0) {
console.log('FizzBuzz');
} else if (num % 5 === 0) {
console.log('Buzz')
} else if (num % 3 === 0) {
console.log('Fizz');
} else {
console.log(num);
}
}
vs.
for (let num = 1; num <= 100; num++) {
if (num % 3 === 0) {
console.log('Fizz');
} else if (num % 5 === 0) {
console.log('Buzz')
} else if (num % 15 === 0) {
console.log('FizzBuzz');
} else {
console.log(num);
}
}

else if basically means "If the previous condition didn't pass THEN IF...". So, in your second snippet, if num % 3 === 0 is true, it will never bother checking if num % 5 === 0 or num % 15 === 0.

Related

Why is my FizzBuzz code not outputting correctly?

function counter(numOne, numTwo) {
for (let i = 0; i <= 100; i++) {
if (i % numOne === 0) {
console.log("Fizz");
}
if (i % numTwo === 0) {
console.log("Buzz");
}
if (i % numOne === 0 && i % numTwo === 0) {
console.log("FizzBuzz");
}
else if (i <= 100 && i !== i % numOne === 0 || i !== i % numTwo === 0) {
console.log(i);
}
}
}
counter(3, 5);
For the else if loop, it should console.log all numbers that are <=100, but are not i % numOne === 0 and i % numTwo === 0. So why are only Fizz, Buzz, and FizzBuzz showing up in the output?
Ok, I didn't want to write an answer but since you're new here, I'll put this in a more meaningful way:
function counter(numOne, numTwo) {
for (let i = 0; i <= 100; i++) {
const isFizz = i % numOne === 0
const isBuzz = i % numTwo === 0
if (isFizz && isBuzz) {
console.log("FizzBuzz");
}
else if (isFizz) {
console.log("Fizz");
}
else if (isBuzz) {
console.log("isBuzz")
}
else {
console.log(i);
}
}
}
counter(3, 5);
In your example, you had:
i !== i % numOne === 0
as stated above, there are two issues here:
i !== i can never be true, it's the same value, it's always i === i or in your case false
Since the above is false, you'll have a math equation of: false % numOne this will result in a NaN and NaN does not equal 0
Hope this and the comments above helps understand your issue

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)

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

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