Ask Your Question
4

How to create a FizzBuzz quiz using JavaScript arrays?

asked 2021-12-21 11:00:00 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2021-06-13 09:00:00 +0000

pufferfish gravatar image

To create a FizzBuzz quiz using JavaScript arrays, you can follow these steps:

  1. Define an array quiz to store the numbers from 1 to 100:
const quiz = Array.from({length: 100}, (_, i) => i + 1);
  1. Use the map() method to iterate through the quiz array and replace the numbers that are divisible by 3 with "Fizz", the numbers that are divisible by 5 with "Buzz", and the numbers that are divisible by both 3 and 5 with "FizzBuzz":
const quizFizzBuzz = quiz.map((num) => {
  if (num % 3 === 0 && num % 5 === 0) {
    return "FizzBuzz";
  } else if (num % 3 === 0) {
    return "Fizz";
  } else if (num % 5 === 0) {
    return "Buzz";
  } else {
    return num;
  }
});
  1. Finally, you can log the quizFizzBuzz array to the console to see the FizzBuzz quiz:
console.log(quizFizzBuzz);

Output:

[1, 2, "Fizz", 4, "Buzz", "Fizz", 7, 8, "Fizz", "Buzz", 11, "Fizz", 13, 14, "FizzBuzz", 16, 17, "Fizz", 19, "Buzz", "Fizz", 22, 23, "Fizz", "Buzz", 26, "Fizz", 28, 29, "FizzBuzz", 31, 32, "Fizz", 34, "Buzz", "Fizz", 37, 38, "Fizz", "Buzz", 41, "Fizz", 43, 44, "FizzBuzz", 46, 47, "Fizz", 49, "Buzz", "Fizz", 52, 53, "Fizz", "Buzz", 56, "Fizz", 58, 59, "FizzBuzz", 61, 62, "Fizz", 64, "Buzz", "Fizz", 67, 68, "Fizz", "Buzz", 71, "Fizz", 73, 74, "FizzBuzz", 76, 77, "Fizz", 79, "Buzz", "Fizz", 82, 83, "Fizz", "Buzz", 86, "Fizz", 88, 89, "FizzBuzz", 91, 92, "Fizz", 94, "Buzz", "Fizz", 97, 98, "Fizz", "Buzz"]
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-12-21 11:00:00 +0000

Seen: 11 times

Last updated: Jun 13 '21