JavaScript Challenge - FizzBuzz

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 (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 (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 (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

def fizzbuzz(limit):

Write your code here

limit = int(limit)
x =
for i in range(1,limit+1):
if i%3==0 and i%5!=0:
x.append(“Fizz”)
elif i%5==0 and i%3!=0:
x.append(“Buzz”)
elif i%3==0 and i%5==0:
x.append(“FizzBuzz”)
else:
x.append(i)
return x

print(fizzbuzz(1500))

1 Like

Had a sense of nostalgia when doing this one!

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

Used some ternary chaining:

function fizzbuzz(n) {
  let arr = [];
  for (let nr = 1; nr <= n; nr++) {
    arr.push(
      (nr % 3 === 0) && (nr % 5 === 0) ? 'FizzBuzz' :
      nr % 3 === 0 ? 'Fizz' :
      nr % 5 === 0 ? 'Buzz' : nr
    )
  }
  return arr;
}
1 Like

Always fun to do this one:

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;

A tried-and-true favorite of mine!

function fizzbuzz(n) {
  // Write your code here
  let nextNum = 1
  let fizzBuzzArray = []
  
  while(nextNum <= n) {
    const isFizz = nextNum % 3 === 0
    const isBuzz = nextNum % 5 === 0
    const isFizzBuzz = isFizz && isBuzz
    const eitherOr = isFizz || isBuzz
    
    if(eitherOr) {
      if(isFizzBuzz) {
        fizzBuzzArray.push("FizzBuzz")
        nextNum++
        continue
      }

      if(isFizz) fizzBuzzArray.push("Fizz")
      if(isBuzz) fizzBuzzArray.push("Buzz")

      nextNum++
      continue
    }

    fizzBuzzArray.push(nextNum)
    nextNum++
  }

  return fizzBuzzArray
}

fizzbuzz(16)

console.log(fizzbuzz(16));

Very basic solution with looping…

function fizzbuzz(n) {
// Write your code here
let result = ;
for(let i = 1; i <= n; i++) {
if(Number.isInteger(i/3) && Number.isInteger(i/5)) {
result.push(“FizzBuzz”);
} else if (!Number.isInteger(i/3) && Number.isInteger(i/5)) {
result.push(“Buzz”)
} else if (Number.isInteger(i/3) && !Number.isInteger(i/5)) {
result.push(“Fizz”)
} else {
result.push(i)
}
}
return result
}

console.log(fizzbuzz(16));

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) {
 let result = [];
 
 for (let i = 1; i <= n; i++) {
   result.push( (i%5 == 0 && i%3 == 0) ? 'FizzBuzz' : 
   (i%3 == 0) ? 'Fizz' : (i%5 == 0) ? 'Buzz' : i);
 }
 return result;
}

console.log(fizzbuzz(16));

// Leave this line for testing:
module.exports = fizzbuzz;

Here is my version!

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;

This is my solution. Not elegant at all but it’s my first Challenge ever!

function fizzbuzz(n) { let myArray = []; for (let i = 1; i < (n + 1); i++) { if ((i % 3 === 0) && (i % 5 === 0)) { myArray.push('FizzBuzz'); } else if (i % 3 === 0) { myArray.push('Fizz'); } else if (i % 5 === 0) { myArray.push('Buzz'); } else { myArray.push(i); } } return myArray; } console.log(fizzbuzz(16));

No division or modulo operation in my version. :see_no_evil:

function fizzbuzz(n)
{
  let result = [];
  for (i = 1; i <= n; ++i)
    result.push([i, "Fizz", "Buzz", "FizzBuzz"][
      ((i * -21845 & 65535) < 21845) | ((i * -13107 & 65535) < 13107) << 1
      ])
  return result;
}

console.log(fizzbuzz(100));

// 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;

I really like your use of the += manipulation of a string value. Seems like a more elegant way to match shared multiples of 5 AND 3.