Files
freeCodeCamp/curriculum/challenges/chinese/22-rosetta-code/rosetta-code-challenges/fizzbuzz.md
2024-01-24 19:52:36 +01:00

1.7 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
5e9ddb06ec35240f39657419 FizzBuzz 1 385370 fizzbuzz

--description--

Write a program that generates an array of integers from 1 to 100 (inclusive). But:

  • for multiples of 3, add "Fizz" to the array instead of the number
  • 对于 5 的倍数,将 "Buzz" 添加到数组而不是数字
  • 对于 3 和 5 的倍数,将 "FizzBuzz" 添加到数组而不是数字

--instructions--

您的程序应该返回一个包含基于上述规则的结果的数组。

--hints--

fizzBuzz 应该是一个函数。

assert(typeof fizzBuzz == 'function');

fizzBuzz() 应该返回一个数组。

assert(Array.isArray(fizzBuzz()) == true);

只能被 3 整除的数字应该返回 "Fizz"

assert.equal(fizzBuzz()[2], 'Fizz');

只能被 5 整除的数字应该返回 "Buzz"

assert.equal(fizzBuzz()[99], 'Buzz');

可被 3 和 5 整除的数字应返回 "FizzBuzz"

assert.equal(fizzBuzz()[89], 'FizzBuzz');

不能被 3 或 5 整除的数字应返回数字本身。

assert.equal(fizzBuzz()[12], 13);

--seed--

--seed-contents--

function fizzBuzz() {

}

--solutions--

function fizzBuzz() {
    let res=[];
    for (let i =1; i < 101; i++) {
        if (i % 3 === 0  && i % 5 === 0) {
            res.push("FizzBuzz");
        }
        else if (i % 3 === 0) {
            res.push("Fizz");
        }
        else if (i % 5 === 0) {
            res.push("Buzz");
        } 
        else {
            res.push(i);
        }
    }
    return res;
}