A PowerShell Counting Challenge

It is time for a new PowerShell scripting challenge. Today’s challenge is targeted at beginner and intermediate skill levels, although everyone is welcome to try their hand. To get the most out of the challenge you should do as much of all challenges posted. And really do it. Commit code to a file and not merely in your head. The best learning comes by actually doing. As always, you are encouraged to post links to your work in the comments.

Beginner

The challenge is simple: get the sum of the even numbers between 1 and 100. You should be able to do this in at least 3 different ways. Show all 3 ways. You don’t need to write any functions or scripts.

Intermediate

After you’ve solved the beginner level, take what you’ve learned and create a PowerShell function to get the sum and average of every X number between 1 and a user specified maximum. For example, the function should produce a result that shows the sum and average of every 4th number between 1 and 512.

Bonus Options

If you want a bit more challenge add the following features to your 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

8 Replies to “A PowerShell Counting Challenge”

  1. thedavecarroll

    It’s been a while since I completed an IronScripter challenge completely or even created a gist or post with my solution. I know that if I had kept skipping them, my skills could diminish.

    I encourage anyone reading this to go through the IronScripter challenges.

    You may feel that some may be below your “level” or capability. If that is the case, the challenge shouldn’t take you long to finish. Your solution, once shared, could be that extra bit that someone needs to gain a better understanding of PowerShell.

    You also may feel that it is above your “level”. Everyone has been at whatever level you believe yourself to currently be. You should push to learn more when possible. Be sure to review past challenge solutions to see how others approached them. It could help you by seeing other’s code.

    Here is my solution:
    https://gist.github.com/thedavecarroll/e18d8a7d6c0f6a51acd8c46aa4a446ed

  2. danubie

    Thank you for the general idea und for that special challenge.
    Within the first minute, it took me back to school days learning maths and listening to stories about mathematical genius’.

    It offered me the possibility to freely think about different solutions to a single problem. And it led me to some very funny hours of thinking, designing, coding, refactoring and testing. Danke.

    At here are my results – I hope they are correct:
    https://github.com/danubie/Ironscripter/tree/master/CountingChallenge

    • Jeffery Hicks

      The idea was that there are at least 3 different ways you could write an answer in PowerShell. Take a look at some of the other posted solutions as way of comparison.

Comments are closed.