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.
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.
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.
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”
This was fun!
Maybe I’ll go back and try some of the harder ones, maybe I wait for a new one to be posted – we’ll see 😉
Here’s what I did:
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:
I have been so busy lately that I haven’t even had time to work on scripts and modules I’ve wanted to get back to. This is the perfect challenge to get me rolling in the right direction again. It was a lot of fun to put this together. I’m going to suggest members of my team at work try this one out. You can see my answers in my Github repo: https://github.com/clsmith70/IronScripter/tree/CountingChallenge.
Here’s what I worked out: https://jdhitsolutions.com/blog/powershell/7494/solving-the-powershell-counting-challenge/
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:
I believe I may be missing the “3 ways” in the beginner section. Not really sure if it was asking for different methods as I kept thinking of int arrays and using modulo (%).
If anyone sees that I am off in how I approached it, please let me know :). Here is my take:
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.