The output of a fizbuzz function is undefined - javascript

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)

Related

Fizzbuzz function logic not working: Output order is incorrect and logic doesn't make sense

I'm creating a simple Javascript Function that should write the numbers from 1 to n. For any multiplier of 3, it outputs "Fizz", instead of multiplers of 5 you output "Buzz", and if they happen at the same time, you should use "FizzBuzz". The output of the function should be a mixed array of numbers and strings.
My current code is outputting the array order and expected values incorrectly.
function fizzbuzz (n) {
let arr = [];
for (let i = 0; i < n; i++){
//if i is a multiple of 3 then Fizz
if(i % 3 === 0) {
arr.push("Fizz");
}
//if i is a multiple of 5 then Buz
if(i % 5 == 0) {
arr.push("Buzz");
}
//if both then FizzBuzz
if(i % 3 === 0 && i % 5 === 0) {
arr.push("FizzBuzz");
}
else {
arr.push(i);
}
}
return arr;
}
When fizzbuzz(7) is entered, I expect the output to look like this:
[0, 1, 2, "Fizz", 4, "Buzz", "Fizz", 7];
Instead, it's this:
["Fizz","Buzz","FizzBuzz",1,2,"Fizz",3,4,"Buzz",5,"Fizz",6]
Could someone enlighten me to the fault in my logic? This should be more straightforward than I had originally thought.
You have three different if statements there, the last one having an else clause, instead of one continuous if statement with several else if clauses. Note that for this to work properly you need to first test the "FizzBuzz" condition:
function fizzbuzz (n) {
let arr = [];
for (let i = 0; i < n; i++){
//if both then FizzBuzz
if(i % 3 === 0 && i % 5 === 0) {
arr.push("FizzBuzz");
}
//if i is a multiple of 3 then Fizz
else if(i % 3 === 0) { // Changed to an else-if
arr.push("Fizz");
}
//if i is a multiple of 5 then Buz
else if(i % 5 == 0) { // Changed to an else-if
arr.push("Buzz");
}
else {
arr.push(i);
}
}
return arr;
}
Quick note regarding the expected output - 0 is divisible by 3 and 5 (and other other integer, for that matter), so the first element of the array should be "FizzBuzz"

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 test a number against two conditions simultaneously with Javascript?

I'm having an issue making my loop test against two conditions simultaneously.
for (var result = 0; result <= 100; result ++)
if (result % 3 == 0)
console.log ('Fizz');
else if (result % 5 == 0)
console.log ('Buzz');
else console.log (result);
How can I build a third condition that tests if result is divisible by 3 AND 5?
else if ( result % 5 == 0 && result % 3 == 0)
console.log ('FizzBuzz');
else console.log (result);
This was my solution and 'FizzBuzz' didn't make it the console at all! Why didn't that solution work?
The order matters!
if (result % 5 == 0 && result % 3 == 0)
console.log ('FizzBuzz');
else if (result % 3 == 0)
console.log ('Fizz');
else if (result % 5 == 0)
console.log ('Buzz');
else console.log (result);
When result is both a multiple of 3 AND a multiple of 5, the following both hold:
result is a multiple of 3
result is a multiple of 5
So if you check result % 5 == 0 && result % 3 == 0 in the else part, it will never be true.
You might also consider nested conditionals:
if (result % 3 == 0)
if (result % 5 == 0)
console.log ('FizzBuzz');
else
console.log ('Fizz');
else if (result % 5 == 0)
console.log ('Buzz');
else console.log (result);
Your last code fragment is missing a bit, but I'd assume it's because you're testing your AND statement in an else if block.
You're saying
if result is a multiple of 3 print fizz
otherwise (not a multiple of 3), if result is a multiple of 5 print buzz
otherwise (not a multiple of 3 nor 5), if result is a multiple of 3 and 5 print fizzbuzz
Obviously, result will never be all of the conditions in part 3 since it can't be
NOT 3x
NOR 5y
AND 3x
AND 5y
at the same time
You have the right idea but the wrong order.
Since the single statements:
if(result % x == 0)
will return before the combined if() statement, and so the condition never gets that far. This worked for me:
for(var result=0; result <=15; result++)
if(result % 5 == 0 && result % 3 ==0)
console.log('FizBuzz');
else if(result % 3 == 0)
console.log('Fizz')
else if(result % 5 == 0)
console.log('Buzz');
else
console.log(result)
this is the catch of the task - you don't need to test against (i % 3 === 0 && i % 5 === 0)
function fizzBuzzTest() {
"use strict";
for (let i = 1; i <= 100; i++) {
let str = "";
if (i % 3 === 0) {
str = "Fizz";
}
if (i % 5 === 0) {
str += "Buzz";
}
if (!str) {
str = i;
}
console.log(str);
}
}
fizzBuzzTest();

Fizz Buzz return value javascript

I'm having an assignment where I have to do the FizzBuzz game in Javascript.
The problem is my loop stops after the first iteration and only return the first value (4). I may be blind to my code but I can't find where the error(s) is.
If you could please push me in the right direction I'd be happy. Thanks in advance. Regards, Thomas.
function fizzBuzz(start, stop) {
for(var i = start; i <= stop; i++) {
if (i % 3 == 0) {
return "fizz";
}else if( i % 5 == 0) {
return "buzz";
}else if(i % 15 == 0) {
return "fizz buzz";
}else {
return i;
}
}
}
ANSWER = (fizzBuzz(4, 22));
New code:
function fizzBuzz(start, stop) {
for(var i = start; i <= stop; i++) {
if (i % 3 == 0 && i % 5 == 0) {
document.write ("Fizz Buzz");
}else if(i % 3 == 0) {
document.write ("Fizz");
}else if(i % 5 == 0) {
document.write ("Buzz");
}else {
document.write(i);
}
}
}
ANSWER = (fizzBuzz(4, 22));
It returns: Answer = undefined
When your code encounters the return statement, the value given is returned from the whole function. This stops the for loop from iterating any further.
These are the questions you'll need to ask yourself:
What do you want to the fizzBuzz function to do? Should it print the text somewhere, or should it return a value?
If fizzBuzz should return a value, what would you expect that it returns? One line of text? Multiple lines of text all at once?
Whatever branch is taken within your for loop, there is always a return which exits the funtion with a return value. So, when you start with 4, the program enters the else branch and returns 4.
What you want is to print the value instead of returning from the function.
Also, I can see a logical error in the code. Assume i is 15, which is divisible by 3 and 5. Your program will go into the i % 3 branch and return "fizz" instead of "fizz buzz". You may want to change your if statements and/or work with string concatenation.
Hope I could help. ;)
One of the best solution in javascript to the fizzbuzz problem
const fizzbuzz = (start, stop) => {
const arr = [];
let str;
for(let i=start; i<=stop; i++) {
str = "";
if(i%3===0) {
str = "fizz";
}
if(i%5===0) {
str += "buzz";
}
arr.push(str||i);
}
return arr;
}
console.log(fizzbuzz(0, 105));

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