📜  FizzBuzz - Javascript (1)

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

FizzBuzz - Javascript

FizzBuzz is a widely-used programming challenge that tests a programmer's understanding of basic loop control structures, arithmetic operations, and condition statements. It is often used as an interview question to assess the problem-solving skills of a candidate.

The Challenge

The challenge is quite simple: write a program that prints the numbers from 1 to 100. But for multiples of three, print "Fizz" instead of the number and for the multiples of five, print "Buzz". For numbers which are multiples of both three and five, print "FizzBuzz".

The Solution

Here is a solution 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);
  }
}

This solution uses a simple for loop to iterate through the numbers from 1 to 100. For each number, it checks if it is a multiple of both 3 and 5 and prints "FizzBuzz" if it is. If it is not, it checks if it is a multiple of 3 and prints "Fizz" if it is. If it is not, it checks if it is a multiple of 5 and prints "Buzz" if it is. If it is none of the above, it simply prints the number.

Conclusion

FizzBuzz is a simple problem that can be solved in many different ways, but it serves as a good way to practice basic programming concepts and problem-solving skills. The solution presented here is one of many possible solutions, and it can be improved or adapted to fit different situations.