Created
May 11, 2020 23:06
-
-
Save thedavecarroll/e18d8a7d6c0f6a51acd8c46aa4a446ed to your computer and use it in GitHub Desktop.
IronScripter Challenge - May 11, 2020 - A PowerShell Counting Challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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}} | |
} |
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
PowerShell Counting Challenge
Check out my blog post for this.