# 412 Fizz Buzz

LeetCode

Write a program that outputs the string representation of numbers from 1 to n.

But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. 
For numbers which are multiples of both three and five output “FizzBuzz”.

input: 給數字  n
output: 印出 n 個, 但 3n (第 3, 6, 9) 會印出 "Fizz", 第 5n 個會印出 "Buzz", 3 跟 5 的倍數會印出 "FizzBuzz" (15n)
Example:

n = 15,

Return:
[
    "1",
    "2",
    "Fizz",
    "4",
    "Buzz",
    "Fizz",
    "7",
    "8",
    "Fizz",
    "Buzz",
    "11",
    "Fizz",
    "13",
    "14",
    "FizzBuzz"
]
*/
/**
 * %3 == 0 "Fizz"
 * %5 == 0 "Buzz"
 * %15 == 0 "FizzBuzz"
 * 沒甚麼特別的題目
 */ 

/**
 * @param {number} n
 * @return {string[]}
 */
var fizzBuzz = function(n) { }

怎麼解

沒甚麼特別的題目,主要就是

  • %3 == 0 ,print "Fizz"

  • %5 == 0 ,print "Buzz"

  • %15 == 0 ,print "FizzBuzz"

var fizzBuzz = function(n) {
  return Array.from({length: n}).map((item, index) => {
    let oneBasedIndex = index + 1
    if(oneBasedIndex%3===0){
      return 'Fizz'
    }
    if(oneBasedIndex%5===0){
      return 'Buzz'
    }
    if(oneBasedIndex%15===0){
      return 'FizzBuzz'
    }
    
    return oneBasedIndex
  })
}

Last updated