📜  fizzbuzz js - Javascript (1)

📅  最后修改于: 2023-12-03 15:00:46.582000             🧑  作者: Mango

FizzBuzz JS - JavaScript

FizzBuzz is a very popular coding challenge used in coding interviews to test a candidate's coding skills. It requires the programmer to print out numbers from 1 to 100, but for each number that is a multiple of 3, print out "Fizz" instead of the number, and for each number that is a multiple of 5, print out "Buzz" instead of the number. If the number is a multiple of both 3 and 5, print out "FizzBuzz" instead of the number.

Here is an example of FizzBuzz in JavaScript:

for (let i = 1; i <= 100; i++) {
  if (i % 3 === 0 && i % 5 === 0) {
    console.log("FizzBuzz");
  } else if (i % 3 === 0) {
    console.log("Fizz");
  } else if (i % 5 === 0) {
    console.log("Buzz");
  } else {
    console.log(i);
  }
}

In this code, we have used a for loop to iterate through the numbers from 1 to 100. We have used the modulo operator (%) to check if the number is divisible by 3 or 5 or both. If it's divisible by both, we print out "FizzBuzz". If it's only divisible by 3, we print out "Fizz", and if it's only divisible by 5, we print out "Buzz". If none of the above conditions are met, we print out the number itself.

This is a simple example, but you can modify it to display numbers from any other range, or to use different words for different multiples. FizzBuzz is a fun and challenging way to practice your coding skills, and it's a great way to showcase your problem-solving abilities in coding interviews.

Happy coding!