FizzBuzz (Super Edition)

Learning Competencies

  • Use strings, integers, arrays, functions
  • Use if/else or switch statements, string methods, for loops

Time Box

ActivityTime
Kata4 hours
Reflect10 minutes

Summary

FizzBuzz is a classic programming exercise (and a reasonably common technical interview question).

The usual example asks the developer to write a program that prints out each number from 1 to 100 but for multiples of 3 print 'Fizz' instead of the number, and for multiples of 5 print 'Buzz'. For numbers that are multiples of both 3 and 5, print 'FizzBuzz'.

// For example:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
// etc.

This exercise has a little twist.

First, write a fizzbuzz function which takes a number as its input. It should return the number that is passed to it, unless the number is:

  • divisible by 15, then it should be replaced with the string "FizzBuzz"
  • divisible by 5, then it should be replaced with the string "Buzz"
  • divisible by 3, then it should be replaced with the string "Fizz"

Next, write a function called superFizzbuzz which takes an array of numbers as its input.

It should loop over that array and then return a "fizzbuzzed" array i.e. the array is identical to the original, but with the following changes:

For example:
superFizzbuzz([1,2,3]) // will return [1, 2, "Fizz"]
superFizzbuzz([1,2,5]) // will return [1, 2, "Buzz"]
superFizzbuzz([1,2,15]) // will return [1, 2, "FizzBuzz"]
superFizzbuzz([30, 9, 20, 1]) // will return ["FizzBuzz", "Fizz", "Buzz", 1]

Plan your code

For this challenge, we're going to start by practising our pseudocode. Before we can code complex functions, we first need to think through the steps we need them to do. This can take some time and brainpower to think through now but saves you time when it comes to writing your code.

To start, think through what you need the function to do and write pseudocode comments for the steps your function will take. These comments will not be written in code but plain English, as if you were talking a friend through the steps.

Write your initial solution

Write code to match your pseudocode steps. You may find you have to do things in a different order than you originally wrote in your pseudocode or add more steps in, and that's alright! The pseudocode is a first go. You don't have to match it exactly.

Run the tests

Once written, run the tests. If your initial solution passes all tests, move on to the next part.