🙆‍♀️

100日アルゴリズム[3日目・FizzBuzz]

2024/03/20に公開

解いた問題

https://leetcode.com/problems/fizz-buzz/description/

回答

function fizzBuzz(n: number): string[] {
    let answer:string[] = [];
    for (let i = 1; i <= n; i++) {
        if(i % 3 == 0 && i % 5 == 0) answer.push("FizzBuzz")
        if(i % 3 == 0 && i % 5 !== 0) answer.push("Fizz")
        if(i % 3 !== 0 && i% 5 == 0) answer.push("Buzz")
        if(i % 3 !== 0 && i % 5 !== 0) answer.push(String(i))
    }
    return answer
};

Discussion