FizzBuzz: Interview Question

September 26, 2018 | Interview Question |

Back to List

Ever since I heard about a popular interview challenge FizzBuzz I have been obssessed with doing my own spin of it. I first heard about this challenge from Tom Scott on YouTube, the goal was to create the classic game of fizzbuzz but allow you to easily add or change the numbers (Click here to view the video).

 

My end goal was to find a solution that could easily be ported to a different language. I believe that I have succeded in making an easily portable function! Here is the function in c# and in javascript to show how easy it is to switch it to another language!

static string FizzBuzz(int fizz, int buzz, int maxNumber)
{
    string output = "";
    for (int i = 1; i <= maxNumber; i++)
    {
        if (i % fizz == 0){output += "fizz";}
        if (i % buzz == 0) { output += "buzz"; }
        if (i % fizz != 0 && i % buzz != 0){output += i + "";}
        output += "\n";
    }
    return output;
}
function fizzbuzz(fizz, buzz, maxNumber){
        var output = document.getElementById('output').innerText = "";
	for (var i = 1; i <= maxNumber; i++){
		if (i % fizz == 0){output += "fizz";}
		if (i % buzz == 0) { output += "buzz"; }
		if (i % fizz != 0 && i % buzz != 0){output += i + "";}
		output += "\n";
	}
	document.getElementById('output').innerText = output;
}

It's simple and doesn't take many lines of code! Below you will find an example of what this function currently does!