Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

FizzBuzzで学ぶOCP

Nokogiri
December 24, 2023

 FizzBuzzで学ぶOCP

Nokogiri

December 24, 2023
Tweet

More Decks by Nokogiri

Other Decks in Technology

Transcript

  1. γϯϓϧͳ FizzBuzz // 3ͷഒ਺ͷ৔߹͸Fizz // 5ͷഒ਺ͷ৔߹͸Buzz // 3ͱ5ͷഒ਺ͷ৔߹͸FizzBuzz // ͱදࣔ͢Δ

    export const fizzBuzz = (num: number): string => { if (num % 3 === 0 && num % 5 === 0) { return "FizzBuzz"; } if (num % 3 === 0) { return "Fizz"; } if (num % 5 === 0) { return "Buzz"; } return ""; }; 3ͷഒ਺ͷ৔߹͸Fizz 5ͷഒ਺ͷ৔߹͸Buzz 3ͱ5ͷഒ਺ͷ৔߹͸FizzBuzz ͦΕҎ֎ͷ৔߹͸"" Λฦ͢
  2. ࢓༷ͷຊ࣭ // 3ͷഒ਺ͷ৔߹͸Fizz // 5ͷഒ਺ͷ৔߹͸Buzz // 3ͱ5ͷഒ਺ͷ৔߹͸FizzBuzz // ͱදࣔ͢Δ export

    const fizzBuzz = (num: number): string => { if (num % 3 === 0 && num % 5 === 0) { return "FizzBuzz"; } if (num % 3 === 0) { return "Fizz"; } if (num % 5 === 0) { return "Buzz"; } return ""; }; ੔਺Λೖྗ͢ΔͱจࣈྻΛฦ ͢ ೚ҙͷม׵ϧʔϧΛෳ਺ఆٛͰ ͖Δ ม׵ϧʔϧ͸ԿΒ͔ͷ൑ఆ৚݅ Λຬͨͨ͠ͱ͖ʹదԠ͞ΕΔ
  3. ίʔυҊ // 3ͷഒ਺ͷ৔߹͸Fizz // 5ͷഒ਺ͷ৔߹͸Buzz // 3ͱ5ͷഒ਺ͷ৔߹͸FizzBuzz // ͱදࣔ͢Δ export

    const fizzBuzz = (num: number): string => { if (num % 3 === 0 && num % 5 === 0) { return "FizzBuzz"; } if (num % 3 === 0) { return "Fizz"; } if (num % 5 === 0) { return "Buzz"; } return ""; }; RuleΛ࡞Δ Matchɿೖྗʹ߹க͢Δ৚ ݅Λදݱ͢Δ apply: ৚݅ʹ߹கͨ͠৔߹ ͷॲཧΛදݱ͢Δ RULE_SETS: దԠ͞ΕΔ ϧʔϧͱॱ൪Λදݱ͢Δ
  4. ίʔυҊ // 3ͷഒ਺ͷ৔߹͸Fizz // 5ͷഒ਺ͷ৔߹͸Buzz // 3ͱ5ͷഒ਺ͷ৔߹͸FizzBuzz // ͱදࣔ͢Δ export

    const fizzBuzz = (num: number): string => { if (num % 3 === 0 && num % 5 === 0) { return "FizzBuzz"; } if (num % 3 === 0) { return "Fizz"; } if (num % 5 === 0) { return "Buzz"; } return ""; }; ֦ுʹΦʔϓϯɿϧʔϧΛ௥ Ճ͢Δ৔߹͸৽͘͠ϧʔϧΛ ఆٛͯ͠RULE_STESʹ௥Ճ ͢Δ͚ͩ มߋʹΫϩʔζυɿϝΠϯͷ ॲཧͰ͋Δ fi zzBuzzʹ͸Өڹ Λ༩͑ͳ͍ɻมߋର৅ͷϧʔ ϧͷΈमਖ਼Ͱ͖Δ