Why is my Javascript fizzbuzz code not working? - javascript

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)

Related

The output of a fizbuzz function is undefined

I have a Javascript code that is not running in the console in Chrome. It is showing "Undefined" when I call the function.
let output = [];
let count = 1;
function fizzBuzz() {
while (count <= 100) {
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++;
}
}
I am expecting it to output an array of numbers up to 100 but inserting "Fizz" for multiples of 3, "Buzz" for multiples of 5, and "FizzBuzz" for multiples of both 3 and 5.
You have the following code
let output =[];
let count = 1;
function fizzBuzz() {
while(count<=100) {
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++;
}
}
which you are executing by calling fizzBuzz(). You're getting undefined because when you call fizzBuzz() in the console you're seeing the return value of the execution, which in your code doesn't ever return anything, so we get undefined.
You have two options.
Return output at the end of your function, outside of the while loop. In this case, it'd be best to also move your output and count variables inside, like so
function fizzBuzz() {
let output =[];
let count = 1;
while(count<=100) {
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++;
}
return output;
}
We move the output and count declarations inside so that every time we call fizzBuzz() we start from scratch. If we kept them outside, then if you called fizzBuzz() twice, we'd see the results from the first call in the results from the second call as well.
Your second option is to call fizzBuzz() with how your code currently is, and then print the value of output. I think you're just making a simple mistake and don't realize that your fizzBuzz doesn't return anything.
fizzBuzz();
console.log(output);
or, if you're executing this in your browsers developer console then
fizzBuzz()
output
You are missing a return statement for the output
let output =[];
let count = 1;
function fizzBuzz() {
while(count<=100) {
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++;
}
return output;
}
console.log(fizzBuzz());
If you want run your code without getting undefined then you can use var instead of using 'let'. Due to the variable block scope you are getting undefined.
Just replace let with var.
Then return output variable in your function body. use console.log(your function name)

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

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