Skip to content

Instantly share code, notes, and snippets.

@rlevchenko
Last active November 15, 2022 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rlevchenko/44737e4d818c813db0d048aa98efeb4f to your computer and use it in GitHub Desktop.
Save rlevchenko/44737e4d818c813db0d048aa98efeb4f to your computer and use it in GitHub Desktop.
function Get-FizzBuzz {
<#
.SYNOPSIS
Fizz Buzz function
.Description
The Fizz Buzz function to find numbers divisible by 3/5 or both
.PARAMETER Start
Specifies the first number
.PARAMETER End
Specifies the last number
.EXAMPLE
PS> Get-FizzBuzz -Start 1 -End 100
With positional parameters specified explicitly
.EXAMPLE
PS> Get-FizzBuzz 1 100
Without specifying parameters name
.EXAMPLE
PS> Get-FizzBuzz -Start 1 -End 100 -Verbose
A bit detailed output enabled
.LINK
https://rlevchenko.com
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $false, Position = 1)]
$start = 1,
[Parameter (Mandatory = $false, Position = 2)]
$end = 100
)
$start..$end|Foreach-Object {
$output = ""
if (($_ % 3 -eq 0) -and ($_ % 5 -eq 0)) {
Write-Verbose "Found FizzBuzz..."
$output += "FizzBuzz: $PSItem"
}#if
elseif ($_ % 3 -eq 0) {
Write-Verbose "Found Fizz..."
$output += "Fizz: $PSItem"
}#elseif
elseif ($_ % 5 -eq 0) {
Write-Verbose "Found Buzz..."
$output += "Buzz: $PSItem"
}#elseif
else {
Write-Verbose "Displaying the rest..."
Write-Host "OutOfScope: $_"
}#else
return $output
}#foreach
}#function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment