FizzBuzz
FizzBuzz
The classic. Walk the numbers 1 to n and, for each, decide what string to add to a list:
- divisible by both 3 and 5 →
FizzBuzz - divisible by 3 →
Fizz - divisible by 5 →
Buzz - otherwise → the number itself, as a string
Check the "both" case first, or a multiple of 15 will be caught by the 3 test alone.
FizzBuzz
คลาสสิก. เดินผ่านเลข 1 ถึง n และสำหรับแต่ละเลข ตัดสินใจว่าจะเพิ่มอะไรลงในลิสต์:
- หารด้วยทั้ง 3 และ 5 ลงตัว →
FizzBuzz - หารด้วย 3 ลงตัว →
Fizz - หารด้วย 5 ลงตัว →
Buzz - อื่นๆ → ตัวเลขเอง ในรูปสตริง
ตรวจสอบกรณี "both" ก่อน, มิฉะนั้นعددที่หารด้วย 15 ลงตัวจะถูกจับโดยเงื่อนไข 3 เพียงอย่างเดียว
Write fizzbuzz(n) returning a list for the numbers 1..n: Fizz if divisible by 3, Buzz if by 5, FizzBuzz if by both, otherwise the number as a string. · เขียน fizzbuzz(n) กลับค่า list สำหรับเลข 1..n: Fizz若 divisible by 3, Buzz if by 5, FizzBuzz if by both, otherwise the number as a string.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่