Problems with Modulo in JavaScript - 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')));
};

Related

Why is my Javascript fizzbuzz code not working?

I am trying to create a JS function that will print Fizz when a number divisible by 3 appears and Buzz if it is divisible by 5 and Fizzbuzz if both 5 and 3 but keeps printing "undefined" on chrome>sources>snippets. I have previously managed to run JS there but now it is just printing "undefined". Please help what is wrong with my code? Thank you in advance
This is my code:
var output = [];
var count = 1;
function fizzBuzz() {
if (count % 3 === 0) {
output.push("Fizz");
}
if (count % 5 === 0) {
output.push("Buzz");
}
if (count % 15 === 0) {
output.push("FizzBuzz");
} else {
output.push(count);
}
count++;
console.log(output);
};
You need to call the function using fizzBuzz(). You'd likely want to do this in a loop until it reaches a certain number.
In the example below, I use a while loop to call the function until count reaches 100. I've also moved the console.log() call to the end of the loop, as it doesn't make sense to log the entire array after each iteration.
Also, consider using else if statements so that Fizz or Buzz are only pushed if FizzBuzz has not been pushed. Otherwise, you may find that all three are pushed instead of just FizzBuzz.
var output = [];
var count = 1;
function fizzBuzz() {
if (count % 15 === 0) {
output.push("FizzBuzz");
}
else if (count % 3 === 0) {
output.push("Fizz");
}
else if (count % 5 === 0) {
output.push("Buzz");
}
else {
output.push(count);
}
count++;
};
while(count < 100) {
fizzBuzz();
}
console.log(output)
Try This
var output = [];
function fizzBuzz(i) {
if (i % 3 == 0 && i % 5 == 0) {
output.push("FizzBuzz");
} else if (i % 3 == 0) {
output.push("Fizz");
} else if (i % 5 == 0) {
output.push("Buzz");
} else {
output.push(i)
}
console.log(output)
};
fizzBuzz(15)
var output = [];
var count = 1;
function fizzBuzz() {
if (count % 3 === 0 && count % 5 === 0) {
output.push("FizzBuzz");
}
else if (count % 3 === 0) {
output.push("Fizz");
}
else if (count % 5 === 0) {
output.push("Buzz");
}
else {
output.push(count);
}
count++;
};
while(count < 100) {
fizzBuzz();
}
console.log(output)

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

Logical Operators in 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.

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

Javascript Fizzbuzz Issue

I'm trying to do some simple tests to help further my javascript knowledge (which is quite fresh). Goal 1 is to print numbers from 1-100 that aren't divisible by 5 or 3.
I tried the following:
for (var i = 1; i <= 100; i ++)
{
if (i%3 !== 0 || i%5 !== 0){
console.log(i);
}
}
This logs EVERY number from 1-100, and I can't tell why. Probably the simplest simplest questions here but it's doing my head in!
I think you mean &&, not ||. With ||, you're basically testing to see if the number is not divisible by 3 or by 5 - only if a number is divisible by both do you reject it (in other words, multiples of 15).
The typical answer to FizzBuzz is:
if( i%3 == 0 && i%5 == 0) FizzBuzz
elseif( i % 3 == 0) Fizz
elseif( i % 5 == 0) Buzz
else number
So to get directly to the number you need for i%3==0 to be false AND i%5==0 to be false. Therefore, you want if( i%3 !== 0 && i%5 !== 0)
Here's a quite simple FizzBuzz function that accepts a range of numbers.
function fizzBuzz(from, to) {
for(let i = from; i <= to; i++) {
let msg = ''
if(i % 3 == 0) msg += 'Fizz'
if(i % 5 == 0) msg += 'Buzz'
if(msg.length == 0) msg = i
console.log(msg)
}
}
fizzBuzz(1, 25)
As for a more complex solution, that's one way you could define a higher order function which generates customized FizzBuzz functions (with additional divisors and keywords)
function fizzBuzzFactory(keywords) {
return (from, to) => {
for(let i = from; i <= to; i++) {
let msg = ''
Reflect.ownKeys(keywords).forEach((keyword) => {
let divisor = keywords[keyword]
if(i % divisor == 0) msg += keyword
})
if(msg.length == 0) msg = i
console.log(msg)
}
}
}
// generates a new function
const classicFizzBuzz = fizzBuzzFactory({ Fizz: 3, Buzz: 5 })
// accepts a range of numbers
classicFizzBuzz(1, 25)
const extendedFizzBuzz = fizzBuzzFactory({ Fizz: 3, Buzz: 5, Bazz: 7, Fuzz: 11 })
extendedFizzBuzz(1, 25)
I attacked this the same was as Niet the Dark Absol:
for (var n = 1; n <= 100; n++) {
if (n % 3 == 0 && n % 5 == 0)
console.log("FizzBuzz");
else if (n % 3 == 0)
console.log("Fizz");
else if (n % 5 == 0)
console.log("Buzz");
else
console.log(n);
}
However, you can also do it this way:
for (var n = 1; n <= 100; n++) {
var output = "";
if (n % 3 == 0)
output += "Fizz";
if (n % 5 == 0)
output += "Buzz";
console.log(output || n);
}
One of the hardest parts of learning JavaScript - or any language - for me is understanding solutions can come in many ways. I like the first example more, but it's always good to keep thinking and look at other options.

Categories

Resources