正解:A,D
First, recall the fizzbuzz rules:
* If n divisible by 3 and not by 5 # return ' fizz ' .
* If n divisible by 5 and not by 3 # return ' buzz ' .
* If n divisible by both 3 and 5 # return ' fizzbuzz ' .
* Otherwise # return ' ' (empty string).
Evaluate each test:
Option A:
let res = fizzbuzz(true);
console.assert(res === ' ' );
In JavaScript, when using arithmetic operations with true, it is coerced to the number 1. A typical implementation of fizzbuzz would treat the argument as a number and compute:
* true % 3 # 1 % 3 # 1
* true % 5 # 1 % 5 # 1
So true is effectively treated as 1, which is divisible by neither 3 nor 5. The function should return ' ' . The assertion res === ' ' will pass.
This test covers the scenario "neither divisible by 3 nor 5".
Option B:
let res = fizzbuzz(3);
console.assert(res === ' ' );
* 3 is divisible by 3 but not by 5.
* According to fizzbuzz rules, fizzbuzz(3) should return ' fizz ' .
* This test expects ' ' , so it is incorrect; the assertion would fail in a correct implementation.
Option C:
let res = fizzbuzz(5);
console.assert(res === ' fizz ' );
* 5 is divisible by 5 but not by 3.
* According to fizzbuzz rules, fizzbuzz(5) should return ' buzz ' .
* This test expects ' fizz ' , so it is incorrect.
Option D:
let res = fizzbuzz(15);
console.assert(res === ' fizzbuzz ' );
* 15 is divisible by both 3 and 5.
* According to fizzbuzz rules, fizzbuzz(15) should return ' fizzbuzz ' .
* This assertion correctly matches the expected result, so it is a proper test case.
Thus, the two test cases that correctly test valid fizzbuzz behavior are:
The answer: A, D
Study Guide / Concept References (no links):
* Modulo operator (%) for divisibility checks
* Truthy/number coercion of boolean true in arithmetic (Number(true) === 1)
* Designing unit tests with console.assert
* Typical fizzbuzz logic structure and expected outputs