Skip to content

Instantly share code, notes, and snippets.

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 thedavecarroll/e18d8a7d6c0f6a51acd8c46aa4a446ed to your computer and use it in GitHub Desktop.
Save thedavecarroll/e18d8a7d6c0f6a51acd8c46aa4a446ed to your computer and use it in GitHub Desktop.
IronScripter Challenge - May 11, 2020 - A PowerShell Counting Challenge
# Intermediate Challenge
# Create a PowerShell function to get the sum and average of every X number between 1 and a user specified maximum
function Get-IntermediateCountingChallenge {
param(
[int]$Step,
[int]$Max
)
$NumberArray = for ($i = 1; $i -le $Max; $i = $i + $Step) { $i }
$NumberArray | Measure-Object -Sum -Average | Select-Object Sum,Average
}
# Bonus Challenge
# Add the following to the intermedidate challenge function
# Limit the interval to a value between 1 and 10
# Add a property to your output that includes all the matching number
# Let the user specify the starting and stopping numbers
function Get-BonusCountingChallenge {
param(
[ValidateRange(1,10)]
[int]$Step,
[int]$Max,
[int]$Begin
)
$NumberArray = for ($i = $Begin; $i -le $Max; $i = $i + $Step) { $i }
$NumberArray | Measure-Object -Sum -Average | Select-Object Sum,Average,@{l='MatchingNumbers';e={$NumberArray}}
}
@thedavecarroll
Copy link
Author

thedavecarroll commented May 11, 2020

PowerShell Counting Challenge

Check out my blog post for this.

@Benjar-Git
Copy link

Thanks for the [ValidateRange()] in your parameters section. I do not normally deal with range validators like this so it is a much more elegant solution than what I did for my solution. Always good to pick little things like that up!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment