The FizzBuzz challenge with PowerShell


What is the “FizzBuzz” interview question?

The “FizzBuzz” challenge is a typical interview question designed to help determine job candidates solving skills to filter out job candidates.

The challenge is to write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”, and the answer should be the following:

1, 2, Fizz, 4. Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz

First approach

The first way, I created a variable containing the range from 1 to 100 by using the range operator 1..100 that easily generates the numbers from 1 to 100. Then used the Foreach-Object cmdlet to loop through each number in the variable. When using the Foreach-Object the variable $_ store the current value of each iteration, then checked to see if the number is multipliable of both three and five then output to the console “FizzBuzz”. Furthermore, checked to see if number was multipliable with 3 and then output “Fizz”, and outputting “Buzz” when multipliable by 5. If no of the conditions are met it prints the numbers to the console.

$Number = 1..100
$Number | ForEach-Object {
    if ($_ % 5 -eq 0 -and $_ % 3 -eq 0)
    {
        Write-Host "FizzBuzz"
    }
    elseif ($_ % 3 -eq 0)
    {
        Write-Host "Fizz"
    }
    elseif ($_ % 5 -eq 0)
    {
        Write-Host "Buzz"
    }
    else {
        $_
    }
}

Second approach

This approach is similar to the above, except a different way to l loop through the numbers of from 1 to 100, by starting with creating variable containing the value of $i=1 and loops through until the variable i is less than or equal to 100, then for each round it goes through the same if statements as above.

for ($i = 1; $i -le 100; $i++) {

    if ($i % 5 -eq 0 -and $i % 3 -eq 0)
    {
        Write-Host "FizzBuzz"
    }
    elseif ($i % 3 -eq 0)
    {
        Write-Host "Fizz"
    }
    elseif ($i % 5 -eq 0)
    {
        Write-Host "Buzz"
    }
    else {
        Write-Host $i
    }
}

Third approach

The third approach is also similar with the above, only different is that I created a variable $Numbers = 1..100 that contains the range, same as the first one, in stead using foreach to loop through each $Number in the number range in the variable $Numbers, then using the same if statements as above.

$Numbers = 1..100
foreach ($Number in $Numbers) {

        if ($Number % 5 -eq 0 -and $Number % 3 -eq 0)
    {
        Write-Host "FizzBuzz"
    }
    elseif ($Number % 3 -eq 0)
    {
        Write-Host "Fizz"
    }
    elseif ($Number % 5 -eq 0)
    {
        Write-Host "Buzz"
    }
    else {
        Write-Host $Number
    }
}

Fourth approach

In the last one, I used a little different approach. To start with I used same approach as in the first example, but instead of using if statements, I used switch. This is equivalent to a series of if statements, but it is simpler when checking many types of conditions. The switch condition lists each condition and an optional state, if the condition is met an action is performed. It’s a great way for adding more conditions to a script or function. Lets say you want to add two more conditions “when multipliable by 7 output Foo and when multipliable by 9 output Bar”, as can be seen in the “Extended version”.

# First version
$Number = 1..100
$Number | Foreach-Object {
    switch ($_) {
        { $_ % 5 -eq 0 -and ($_ % 3 -eq 0) } { "FizzBuzz" }
        { $_ % 3 -eq 0} { "Fizz" }
        { $_ % 5 -eq 0} { "Buzz" }
        Default {
            $_
        }
    }
}

# Extended version
$Number = 1..100
$Number | Foreach-Object {
    switch ($_) {
        { $_ % 5 -eq 0 -and ($_ % 3 -eq 0) } { "FizzBuzz" }
        { $_ % 3 -eq 0} { "Fizz" }
        { $_ % 5 -eq 0} { "Buzz" }
    { $_ % 7 -eq 0} { "Foo" }
    { $_ % 9 -eq 0} { "Bar" }
        Default {
            $_
        }
    }
}

That’s for it tonight, have a great one!


For More Content See the Latest Posts