After reading way too much into what fizzbuzz was I though PowerShell could easily have its way with this.
Function Get-FizzBuzz{
param(
[Parameter(Position=0,ValueFromPipeline=$true)]
[int]$Number
)
process{
$result = switch($Number){
{$_ % 3 -eq 0}{"Fizz"}
{$_ % 5 -eq 0}{"Buzz"}
default{$_}
}
-join $result
}
}
What we have is a function that accepts pipeline input. For each number that is passed we run it through a switch. Be default it will check against both clauses. So when it hit 15 it will run for Fizz and Buzz. If neither are matched then the number just continues on without being converted. Since FizzBuzz must appear on the same line we capture the $result
and use -join
to concatenate the elements. This only has an effect on the $result
when it has more than element.
Sample Output
PS C:\Windows\system32> Get-FizzBuzz -Number 15
FizzBuzz
PS C:\Windows\system32> 1..15 | Get-FizzBuzz
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
Not sure what could be done beyond this. I suppose you could argue that a filter as supposed to a function would be more terse. I leave that to a reviewer to chew on.
FizzBuzz
, notFizz\nBuzz
for multiples of both \$\endgroup\$