Ad
Fundamentals
Strings
Data Types
Logic

Maintainable version of FizzBuzz

Code
Diff
  • public class FizzBuzz
     {
       public string GetOutput(int number)
       {
         var ret = string.Empty;
         
         if (number % 3 == 0)
           ret += "Fizz";
         if (number % 5 == 0)
           ret += "Buzz";
         
         if (ret.Length == 0)
           ret = number.ToString();
         return ret;
       }
    }
    • public class FizzBuzz
    • {
    • public string GetOutput(int number)
    • {
    • bool fizz = number % 3 == 0;
    • bool buzz = number % 5 == 0;
    • return fizz && buzz ? "FizzBuzz" : fizz ? "Fizz" : buzz ? "Buzz" : number.ToString();
    • var ret = string.Empty;
    • if (number % 3 == 0)
    • ret += "Fizz";
    • if (number % 5 == 0)
    • ret += "Buzz";
    • if (ret.Length == 0)
    • ret = number.ToString();
    • return ret;
    • }
    • }