January 18, 2007 at 11:21 PM by Dr. Drang
I’ve been thinking about programming languages lately. For years, I wrote all my little utility programs (like the address label and file folder label programs) in Perl, but Ruby seems very attractive and certainly has a lot of momentum behind it now. Unfortunately, I’m not getting much practice with Ruby because I just don’t have many tasks I need to automate right now. Rewriting my old Perl utility programs in Ruby—while it may have some pedagogical value—just seems a waste of time and I can’t work up any interest in doing it.
Since my son is also learning about encryption in math—the teacher is using secret codes as the motivation for several topics—I thought I’d work up a web page that does a simple Caesar shift and use it to explain to him how the process he does with an encryption wheel can be translated to a programming language.
Here’s the page:
The script contains two functions,
doCaeser, which may seem like a bit of overkill, but I wanted the function that actually does the encryption to be separate from the one that does all the communication with the form elements.
-17 % 3 = -2. (Yes, I just wrote a Fortran program to check, the first one in over 25 years. Maybe closer to 30 years. It felt weird writing Fortran in a text editor instead of on a keypunch machine.)
(Image lifted from this wonderful page.)
The only other thing of note is the
onsubmit="return false;" attribute in the
<form> tag. It prevents the browser from “submitting” the form if you hit the Return or Enter key while typing in the shift amount. Without that attribute, an inadvertent Return or Enter will cause the page to reload, erasing whatever secret message you’ve typed into the input field.
How does this work as a teaching aid? I have no idea. Long days at work for men and other homework for my son have kept us from talking about the program. He should understand the HTML section and I’m going to try to get him to ignore the CSS section. I think I’ll have him encrypt and decrypt a short message by hand and we’ll talk about how each step he does corresponds to a line or set of lines in the program. He should understand the modulus operator, because he recently had some homework where he need to figure out the remainder after integer division. We’ll see how it goes.
Update I should have mentioned another difference in how I would have written the program if it were not aimed at a 10-year-old audience: I would have used the
fromCharCode to convert the characters to numbers rather than creating an
alphabet variable and using
charAt. And in fact, that’s how I originally wrote the
alphabet string is much more in keeping with how the kids are learning to encrypt by hand.