This community-built FAQ covers the “FizzBuzz” code challenge in JavaScript. You can find that challenge here, or pick any challenge you like from our list.
Top Discussions on the JavaScript challenge FizzBuzz
There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in #get-help.
Agree with a comment or answer? Like () to up-vote the contribution!
function fizzbuzz(n) {
let arr = [];
for (let i = 1; i <= n; i++) {
let fizz = i % 3 === 0;
let buzz = i % 5 === 0;
let fizzbuzz = fizz && buzz;
let result = i;
// fizz buzz
if (fizzbuzz) {
result = "FizzBuzz"
}
// fizz
else if (fizz) {
result = "Fizz";
}
// buzz
else if (buzz) {
result = "Buzz";
}
// default
arr.push(result);
}
return arr;
}
function fizzbuzz(n) {
// Write your code here
let array = ;
for (let i = 1; i <= n; i++) {
if (i % 15 === 0) {
array.push(“FizzBuzz”);
} else if (i % 3 === 0) {
array.push(“Fizz”);
} else if (i % 5 === 0) {
array.push(“Buzz”);
} else {
array.push(i);
}
}
return array;
}
console.log(fizzbuzz(16));
// Leave this line for testing:
module.exports = fizzbuzz;
function fizzbuzz(n) {
// Write your code here
const arr = [];
for (let i=1; i<=n; i++) {
if (i % 3 === 0 && i % 5 !== 0) {
arr.push('Fizz');
} else if (i % 5 === 0 && i % 3 !== 0) {
arr.push('Buzz');
} else if (i % 3 === 0 && i % 5 === 0) {
arr.push('FizzBuzz');
} else {
arr.push(i);
}
}
return arr;
}
console.log(fizzbuzz(16));
// Leave this line for testing:
module.exports = fizzbuzz;
Okay, I tried this challenge and that’s my code. Since I’m a real beginner I would like to ask a probably dumb question (sorry in advance ^^): when I run the code the array logs in vertical. I there a way to log it in horizontal instead but keeping it asa an array an not turning into a string using the .join() method?
function fizzbuzz(n) {
// Write your code here
const arr = [];
let val;
for (let i = 1; i <= n; i++) {
val = "";
if (i % 3 === 0) {
val += "Fizz";
}
if (i % 5 === 0) {
val += "Buzz";
}
val = val ? val : i;
arr.push(val);
}
return arr;
}
console.log(fizzbuzz(16));
// Leave this line for testing:
module.exports = fizzbuzz;
I did the same basics I think… Do you guys think it’s too naive?
const fizzbuzz = n => {
let solArr = [];
for (let i=1; i<=n; i++) {
if (i%3 && i%5) {
solArr.push(i);
} else if (!(i%3) && !(i%5)) {
solArr.push('FizzBuzz');
} else if (!(i%3)) {
solArr.push('Fizz');
} else if (!(i%5)) {
solArr.push('Buzz');
}
}
return solArr;
} // Have a nice day
console.log(fizzbuzz(16));
// Leave this line for testing:
module.exports = fizzbuzz;
Hi @filippoc.2257100509 ,
it took me a while to figure this out. It is just the editor breaking the array apart because the window is narrow. If you did fizzbuzz(2) they will probably both appear on the same line for you.
function fizzbuzz(n) {
// Write your code here
let results = new Array();
for (let i = 1; i <= n; i++)
{
let f = '';
if (i%3 == 0)
{
f = 'Fizz';
}
if (i%5 == 0)
{
f = f + 'Buzz';
}
if (f.length == 0)
{
results.push(i)
} else {
results.push(f);
}
}
return results;
}
console.log(fizzbuzz(16));
// Leave this line for testing:
module.exports = fizzbuzz;