Edit: I said I would not, but I added more to the final function at the bottom of this post.
My daughter entered the office right on time — the end of my workday yesterday — and asked me to give her math problems. That means multiplication facts. While she is all but done learning her multiplication tables these days, I guess we are still practicing. It was my job to randomly choose two numbers between 1 and 12 that she would multiply in her head and say the answer out loud. I was sitting an arm’s length away from Windows Terminal and PowerShell, so I was about to give up coming up with the two numbers myself. PowerShell was going to do this for me.
This is not the first time I have discussed multiplication this school year. Here is a related post. This post is not really a part 2.
I didn’t want to spend much of our time writing PowerShell, so I quickly wrote the below PowerShell and we started. Because she could see the problems in the console — I zoomed in like I never zoomed in before — we were able to burn through at least a hundred, maybe two before we wrapped up our “give me math” session.
Clear-Host; "$(1..12 | Get-Random) x $(1..12 | Get-Random)"
Here is a quick video that is probably worth seeing at this point in the post.
As we went through these random multiplication problems, my mind kept going to how I would have written this had I had more time to prepare. Well, that is what this post is for now. I know I can add a ridiculous amount of features, sure, but I have other things to do, so I am going to keep the features to a minimum. It did dawn on me to do something like my 1 to 100 game, but again, I only want to give her enough to play it herself. I did not need to keep score or be as nearly as polished as that one.
$1st = Get-Random -InputObject (1..12) $2nd = Get-Random -InputObject (1..12) "$1st x $2nd = $($1st * $2nd)" 3 x 8 = 24
The above example shows some changes to what I had authored previously. Basically, I can create a multiplication problem and include the answer too. This is not very helpful, as my daughter would be able to see the answer. Either way, this was a logical step toward the next example.
In this example, we add a do-until
loop, hiding the product from the user. The Read-Host
prompt will stop promoting for the answer as soon as it is entered correctly.
$1st = Get-Random -InputObject (1..12) $2nd = Get-Random -InputObject (1..12) "$1st x $2nd = ?" do {$Product = Read-Host -Prompt 'Enter Product'} until ($Product -eq $1st * $2nd) 1 x 8 = ? Enter Product: 8 6 x 8 = ? Enter Product: 45 Enter Product: 50 Enter Product: 48
And that was it. Plenty more could have been added, however, I am not sure she would even appreciate additional features and the extra work. This PowerShell will be good enough. I bet she will think it is great.
…
Okay fine, I decided to add a little more, but I am done after this, I swear. I put it in a function with a couple of parameters and here it is. As you will see, not much more time was spent doing this.
Function Test-Multiplication { [CmdletBinding()] Param ( [Parameter()] $FirstRange = (1..12), [Parameter()] $SecondRange = (1..12) ) $1st = Get-Random -InputObject $FirstRange $2nd = Get-Random -InputObject $SecondRange "$1st x $2nd = ?" do {$Product = Read-Host -Prompt 'Enter Product'} until ($Product -eq $1st * $2nd) }
Test-Multiplication 9 x 9 = ? Enter Product: 81 Test-Multiplication 4 x 6 = ? Enter Product: 21 Enter Product: 24
Last note here. Because we have parameters, we can, if we want, send in different ranges for the FirstRange
and SecondRange
parameters. Ony want to practice your 4’s and you can do this.
Test-Multiplication -FirstRange (4..4) 4 x 1 = ? Enter Product: 4 Test-Multiplication -FirstRange (4..4) 4 x 6 = ? Enter Product: 24
Okay, I am done now — for real.
Update: Well, it was not for real. I lasted a night. My daughter played the multiplication game and it was clear it needed one more thing. Even though she, at 10, now knows to press the up arrow for the last command, I went ahead and added a little more. Here is the updated function first and then an example of it being executed.
Function Test-Multiplication { [CmdletBinding()] Param ( [Parameter()] $FirstRange = (1..12), [Parameter()] $SecondRange = (1..12) ) do { $1st = Get-Random -InputObject $FirstRange $2nd = Get-Random -InputObject $SecondRange "$1st x $2nd = ?" do {$Product = Read-Host -Prompt 'Enter Product'} until ($Product -eq $1st * $2nd) do { $Replay = Read-Host -Prompt 'Enter = More (Q = quit)' } until ($Replay -eq '' -or $Replay -eq 'Q') } until ($Replay-eq 'Q') }
[PS7.2.1][C:\] Test-Multiplication 4 x 3 = ? Enter Product: 12 Enter = More (Q = quit): 5 x 10 = ? Enter Product: 50 Enter = More (Q = quit): 4 x 5 = ? Enter Product: 20 Enter = More (Q = quit): 9 x 4 = ? Enter Product: 35 Enter Product: 39 Enter Product: 36 Enter = More (Q = quit): q [PS7.2.1][C:\]
Okay, I am done again, for now. Ugh.