Skip to content

Instantly share code, notes, and snippets.

@phenotypic
Last active July 31, 2020 15:53
Show Gist options
  • Save phenotypic/76a9afdc844c7d1c13da75b6e2ca338b to your computer and use it in GitHub Desktop.
Save phenotypic/76a9afdc844c7d1c13da75b6e2ca338b to your computer and use it in GitHub Desktop.
FizzBuzz in multiple languages

FizzBuzz

My aim was to make multiple variations of the FizzBuzz game in various programming languages. Each language has different approaches ranging in simplicity.

Bash

Approach 1

Test for each of the three possibilities, echoing the number if none of them are met:

for x in {1..100}; do                     # Increment $x by 1 in each repeat (up to 100)
  if (( $x % 15 == 0 )); then             # If $x is divisible by 15 (3 and 5) then echo "FizzBuzz"
    echo "FizzBuzz"
  elif (( $x % 3 == 0 )); then            # If $x is divisible by 3 then echo "Fizz"
    echo "Fizz"
  elif (( $x % 5 == 0 )); then            # If $x is divisible by 5 then echo "Buzz"
    echo "Buzz"
  else                                    # If none of the conditions are met, echo $x
    echo "$x"
  fi
done

Approach 2

String approach, adding to the variable. Better than Approach 1 and allows for easy future integration of more conditions:

for x in {1..100}; do                     # Increment $x by 1 in each repeat (up to 100)
  string=""                               # Reset $string
  if (( $x % 3 == 0 )); then              # If $x is divisible by 3 then add "Fizz" to $string
    string+="Fizz"
  fi
  if (( $x % 5 == 0 )); then              # If $x is divisible by 5 then add "Buzz" to $string
    string+="Buzz"
  fi
  if [ -z "$string" ]; then               # If $string is empty, echo $x
    echo $x
  else                                    # Otherwise, echo $string
    echo $string
  fi
done

Approach 3

Another string approach just like Approach 2, but far more compact and efficient:

for x in {1..100}; do                     # Increment $x by 1 in each repeat (up to 100)
  string=""                               # Reset $string
  (( $x % 3 == 0 )) && string+="Fizz"     # If $x is divisible by 3 then add "Fizz" to $string
  (( $x % 5 == 0 )) && string+="Buzz"     # If $x is divisible by 5 then add "Buzz" to $string
  echo ${string:-$x}                      # If $string is empty, echo $x. Otherwise, echo $string
done

Python

Approach 1

Test for each of the three possibilities, printing the number if none of them are met:

for x in range(1, 101):                   # Increment 'x' by 1 in each repeat (up to 100)
  if x % 15 == 0:                         # If 'x' is divisible by 15 (3 and 5) then print "FizzBuzz"
      print("FizzBuzz")
  elif x % 3 == 0:                        # If 'x' is divisible by 3 then print "Fizz"
      print("Fizz")
  elif x % 5 == 0:                        # If 'x' is divisible by 5 then print "Buzz"
      print("Buzz")
  else:                                   # If none of the conditions are met, print 'x'
      print(x)

Approach 2

String approach, adding to the variable. Better than Approach 1 and allows for easy future integration of more conditions:

for x in range(1, 101):                   # Increment 'x' by 1 in each repeat (up to 100)
  string = ""                             # Reset 'string'
  if x % 3 == 0:                          # If 'x' is divisible by 3 then add "Fizz" to 'string'
    string += "Fizz"
  if x % 5 == 0:                          # If 'x' is divisible by 5 then add "Buzz" to 'string'
      string += "Buzz"
  if string == "":                        # If 'string' is empty, print 'x'
      print(x)
  else:                                   # Otherwise, print 'string'
      print(string)

Approach 3

Another string approach just like Approach 2, but far more compact and efficient:

for x in range(1, 101):                   # Increment 'x' by 1 in each repeat (up to 100)
  string = ""                             # Reset 'string'
  if x%3 == 0 : string += "Fizz"          # If 'x' is divisible by 3 then add "Fizz" to 'string'
  if x%5 == 0 : string += "Buzz"          # If 'x' is divisible by 5 then add "Buzz" to 'string'
  print(x if not string else string)      # If 'string' is empty, print 'x'. Otherwise, print 'string'

JavaScript

Approach 1

Test for each of the three possibilities, printing the number if none of them are met:

for (i = 1; i <= 100; i++) {              // Increment 'i' by 1 in each repeat (up to 100)
  if (i % 15 == 0) {                      // If 'i' is divisible by 15 (3 and 5) then output "FizzBuzz"
    console.log("FizzBuzz")
  } else if (i % 3 == 0) {                // If 'i' is divisible by 3 then output "Fizz"
    console.log("Fizz")
  } else if (i % 5 == 0) {                // If 'i' is divisible by 5 then output "Buzz"
    console.log("Buzz")
  } else {                                // If none of the conditions are met, print 'i'
    console.log(i)
  }
}

Approach 2

String approach, adding to the variable. Better than Approach 1 and allows for easy future integration of more conditions:

for (i = 1; i <= 100; i++) {              // Increment 'i' by 1 in each repeat (up to 100)
  var string = "";                        // Reset 'string'
  if (i % 3 == 0) {                       // If 'i' is divisible by 3 then add "Fizz" to 'string'
    string += "Fizz"
  }
  if (i % 5 == 0) {                       // If 'i' is divisible by 5 then add "Buzz" to 'string'
    string += "Buzz"
  }
  if (string) {                           // If 'string' isn't empty, output it
    console.log(string)
  } else {
    console.log(i)                        // Otherwise, output 'i'
  }
}

Approach 3

Another string approach just like Approach 2, but far more compact and efficient:

for (i = 1; i <= 100; i++) {              // Increment 'x' by 1 in each repeat (up to 100)
  string = "";                            // Reset 'string'
  if (i % 3 == 0) {string += "Fizz"}      // If 'i' is divisible by 3 then add "Fizz" to 'string'
  if (i % 5 == 0) {string += "Buzz"}      // If 'i' is divisible by 5 then add "Buzz" to 'string'
  console.log(string || i)                // If 'string' is empty, print 'i'. Otherwise, print 'string'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment