📅  最后修改于: 2023-12-03 15:01:06.049000             🧑  作者: Mango
Welcome to HackerRank.com FizzBuzz JavaScript challenge. As a programmer, you must have come across the FizzBuzz problem. This challenge is available on HackerRank.com, an online platform for programmers to practice coding and compete with others for the highest score.
The FizzBuzz problem is a basic programming question that requires the programmer to output the numbers from 1 to a specified limit. For any number that is a multiple of three, the output will be "Fizz". For any number that is a multiple of five, the output will be "Buzz". For any number that is a multiple of both three and five, the output will be "FizzBuzz".
Your task is to write a program in JavaScript, that accepts an integer as input and outputs the appropriate FizzBuzz sequence to the given limit.
The input consists of a single integer n (1 ≤ n ≤ 100), which indicates the upper limit of the FizzBuzz sequence.
n = 15
The output must consist of the FizzBuzz sequence from 1 to n, with each number separated by a space.
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz
You can solve this problem using JavaScript by using a for-loop to iterate from 1 to the specified limit, and checking whether the current number is a multiple of 3, 5 or both using the modulo operator (%).
function fizzBuzz(n) {
// Iterate from 1 to n
for (let i = 1; i <= n; i++) {
// Check whether i is a multiple of both 3 and 5
if (i % 3 === 0 && i % 5 === 0) {
console.log("FizzBuzz");
}
// Check whether i is a multiple of 3 only
else if (i % 3 === 0) {
console.log("Fizz");
}
// Check whether i is a multiple of 5 only
else if (i % 5 === 0) {
console.log("Buzz");
}
// Otherwise, print the number
else {
console.log(i);
}
}
}
The HackerRank.com FizzBuzz JavaScript challenge is a great way to practise your programming skills and learn new things. By solving this problem, you will sharpen your JavaScript skills and understand the logic behind the FizzBuzz problem.