Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Switch not as bad as if elseif else
#region Normal way
[int]$FizzRunningTotal = 0
[int]$FizzBuzzRunningTotal = 0
[int]$BuzzRunningTotal = 0
# Works
1..15 | ForEach-Object {
switch ($_) {
{ $_ % 15 -eq 0 } {
$FizzBuzzRunningTotal = $FizzBuzzRunningTotal + $_
break
}
{ $_ % 5 -eq 0 } {
$BuzzRunningTotal = $BuzzRunningTotal + $_
break
}
{ $_ % 3 -eq 0 } {
$FizzRunningTotal = $FizzRunningTotal + $_
break
}
}
[PSCustomObject]@{
Number = $_
FizzRunningTotal = $FizzRunningTotal
BuzzRunningTotal = $BuzzRunningTotal
FizzBuzzRunningTotal = $FizzBuzzRunningTotal
}
} | Format-Table -AutoSize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.