fizzbuzz with psuedocode

Here’s a small experiment inspired by literate programming, future languages that aren’t plain text, and the occasional need for the same thing to be implemented in different languages. The idea is that code could can as an outline in a rich text editor and then be converted step by step into code. In the first step is psuedocode that reads well, at least to me, and at the end are working examples in JavaScript and Python.

Step 1

  • for each number n from 1 to 100, inclusive
    • let s be an empty string
    • if n is evenly divisible by 3, append “Fizz” to s
    • if n is evenly divisible by 5, append “Buzz” to s
    • if s is empty, print n
    • if s is not empty, print s

Step 2 (JavaScript)

  • for (let n=1; n <= 100; n++)
    • let s = ”
    • if (n % 3 == 0) s += ‘Fizz’
    • if (n % 5 == 0) s += ‘Buzz’
    • if (s == ”) console.log(n)
    • if (s != ”) console.log(s)

Step 2 (Python)

  • for n in range(1, 101)
    • s = ”
    • if n % 3 == 0
      • s += ‘Fizz’
    • if n % 5 == 0
      • s += ‘Buzz’
    • if s == ”
      • print(n)
    • if s != ”
      • print(s)

Step 3 (JavaScript)

for (let n = 1; n <= 100; n++) {
  let s = ''
  if (n % 3 == 0) s += 'Fizz'
  if (n % 5 == 0) s += 'Buzz'
  if (s == '') console.log(n)
  if (s != '') console.log(s)
}

Step 3 (Python)

for n in range(1, 101):
    s = ''
    if n % 3 == 0:
        s += 'Fizz'
    if n % 5 == 0:
        s += 'Buzz'
    if s == '':
        print(n)
    if s != '':
        print(s)

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.