Archive for September, 2007

Sydney’s First Overnight Slumber Party

Sunday, September 30th, 2007

Sydney had her first overnight slumber party last night. She stayed overnight with two other four year olds. She did very well. They had a good time making tie dyed t-shirts and going to see the movie “The Game Plan“. (She liked the bull dog.) I definitely give kudos to the parents where she stayed. They had a gaggle of girls sleep over. (They have three girls, and it was the oldest girl’s birthday. She had some friends over for her birthday, but the middle and youngest girl both invited a couple friends to stay overnight too! I think that this is foreshadowing of my future. But I only have two girls. Whew!) The “sleep over” door is open. Sydney is growing up so fast.

[Updated 10/01/07]: I forgot to post this. When talking to the mom in charge of the sleep over when I picked Sydney up, she relayed this funny little story. I guess she got a full dose of Syd. That night, the mom was in with the three four year olds, getting ready to sleep. Sydney was in a giggling mood and kept giggling. The mom said to the effect of “Okay, girls. It’s time to go to sleep and stop the giggling.” Sydney said, without missing a beat, “But giggling is what little girls do.” This is classic Syd.

To the moon, Cringely, to the moon!

Sunday, September 30th, 2007

Robert Cringely announced in his latest column that he plans to participate in the Google Lunar X Prize. The competition challenge is: “soft-landing a robotic craft on the Moon and roving on the surface”. I have been reading Cringely’s column for a long time. I watched him in his PBS show “Plane Crazy“. I wish him well. It would be so cool to work on his team for the challenge. I am sure that he will provide progress reports in his column. I expect them to be entertaining, to say the least. 🙂

“Built in” text field calculator

Wednesday, September 26th, 2007

I have a little web application that I wrote to balance my check book. I often enter a calculated value into a form text field. I usually use the Google Calculator in the Firefox Search Bar to perform the calculations. I often perform simple calculations like “25 * .03”. (Why? I impose a personal finance fee on each credit card purchase that I make. I use a 3% fee. I put all of my personal finance fees into an online high-interest savings account. So, if I charge $25.00 on a card, I transfer “25 * .03” or $0.75 to my online savings account. It isn’t much, but it adds up.) So, yesterday, I thought, “I wonder if I can integrate Google Calculator functionality into my web application?”

Here is what I came up with.

Calculator example.

You can take a look at the source of the “calculator.html“. It includes “JPetersonCalculator100.js“, which is where the JavaScript that actually implements the calculator is sourced. The calculator function is attached to the text input field using an onKeyPress event handler in the HTML:

onkeypress="processCalculatorKeyPress(event, 2);"

Here is the processCalculatorKeyPress function.

001: /**
002:  * Handle a key press from a text field that can automatically calculate a
003:  * derived value. Basically, enter normal mathematical formula like
004:  * "5 * .03 =". When you press the equal ("=") key, the value in the text field
005:  * will be replaced with the calculated value. If the value can't be
006:  * calculated, it is left unchanged.
007:  *
008:  * @param e the event
009:  * @param precision the decimal precision to round to.
010:  */
011:  function processCalculatorKeyPress(e, precision) {
012:   var targ;
013:   if (!e) var e = window.event;
014:   if (e.target) targ = e.target;
015:   else if (e.srcElement) targ = e.srcElement;
016:   if (targ.nodeType == 3) // defeat Safari bug
017: 	targ = targ.parentNode;
018:
019:   if (e.keyCode) code = e.keyCode;
020:   else if (e.which) code = e.which;
021:
022:   if (code == 61) { // '='
023:     var expression;
024:     var result;
025:     expression = targ.value;
026:
027:     try {
028:       // calculate, evaluating arithmetic expression
029:       result = eval(expression);
030:
031:       if (typeof result == "number") {
032:         // round, if necessary
033:         if (typeof precision == "number") {
034:           if (precision > 0) {
035:             if (result.toFixed) {
036:               result = result.toFixed(precision);
037:             } else {
038:               var factor = Math.pow(10,precision);
039:               result = Math.round(result * factor) / factor;
040:             }
041:           }
042:         }
043:
044:         targ.value = result;
045:
046:         // don't display key that triggered the calculation
047:         if (e.preventDefault) {
048:           e.preventDefault();
049:         } else {
050:           e.returnValue = false; // IE
051:         }
052:       }
053:     } catch (e) {
054:       // expression wasn't an arithmetic expression, let normal text be displayed
055:     }
056:   }
057:
058:   // var character = String.fromCharCode(code);
059:   // alert('Code was: ' + code + ' Character was: [' + character + '] targ: [' + targ.value + ']');
060:
061:   return false;
062: }

Lines 12 -10 process the event, makeing a “normalized” version that handles differences in browser implementations. Since the function processCalculatorKeyPress is called for EVERY key press, line 22 filters out all key presses except for the one that we care about: the equals (“=”) key. Line 25 saves the value currently entered into the text field. Line 29 uses the JavaScript eval function to evaluate the arithmetic expression. (This is the actual calculator. The rest of the function make the results look pretty.) The “try”, on line 27, and associated “catch”, on line 53, are there to handle any error thrown by the “eval” function. An error would be thrown if the value entered into the test field was not an arithmetic expression. (Or more generally, not a valid JavaScript expression, since “eval” found evaluate any valid JavaScript expression. But that’s not really useful for the calculator.) Line 31 makes sure that the result of the “eval” is a number. Note that execution will only reach line 31 if “eval” didn’t throw an exception. Lines 33 through 41 perform any required rounding of the result. If available, line 36 uses the “toFixed” function to convert the number to the requested number of decimal places, rounding and padding with zero as necessary. If this function is not available, lines 38 and 39 perform rounding without any padding. Line 44 assigns the result of the arithmetic expression to the text input form element. Lines 47-51 indicate that the calculation succeeded so that the equals sign isn’t added to value of the text input field. Line 58 and 59 are commented out. But they illustrate how you can use an alert box to see what characters are pressed. I used this technique when developing the function.

That’s it. It should be relatively easy for you to include this build in text field calculator into your own web application. The basic steps are:

  1. Include the JavaScript function “processCalculatorKeyPress”.
  2. Add the “onkeypress” to the text fields that you would to support the build in calculator functionality. You can configure the precision value, from no rounding (use a negative value, like “-1”) to whatever decimal place is important. For currency input, I use a precision of “2”.

Lemon-Anise Biscotti

Saturday, September 1st, 2007

Lemon-Anise BiscottiI baked Lemon-Anise Biscotti for the first time on Thursday night. The recipe is from the cook book

Italian Classics by the editors of Cook’s Illustrated magazine.

The recipe is very simple and doesn’t require too many pieces of equipment. It is basically made with flour, baking powder, salt, sugar, eggs, vanilla extract, lemon zest, and anise seed. The equipment required is an oven, a mixing bowl, whisk, and baking sheet with parchment paper. Biscotti are twice-baked Italian cookies. Once the batter is mixed, it is formed into a long loaf shape and baked. It is then sliced into individual pieces and baked again. This recipe doesn’t use butter, so the cookies should last a couple weeks if stored in an airtight container.

They are a relatively hard biscuit. They are perfect dunked in my favorite tea. The lemon and anise flavors are a unique taste that goes well with the English Breakfast tea that I prefer. The cookies have a nice amount of sweetness too. I would make this recipe again.

Magazine Salesman

Saturday, September 1st, 2007

I bought magazines from a door-to-door salesman yesterday. I hope it is for a good cause. (I hope that I didn’t get taken.) He was a nice young man and presented himself well. He said that he was from Chicago and was trying to learn skills to help him have a better life. I am supposed to get 36 issues of Condé Nast Traveler for $60.00 plus a $10.00 processing and handling fee for a total of $70.00. I know that I could have gotten it cheaper by subscribing from Condé Nast directly, so hopefully the extra money goes towards the salesman and not just to Midwest Clearing, Inc., the company fulfilling the subscription.

Running headphones: Nike Flight

Saturday, September 1st, 2007

Nike Flight Sport headphonesYep, the stock earbuds that came with the Sansa Express didn’t work out well while running. They popped out of my ears after a few strides. I found these at my local Target store: Nike Flight Sport headphones. They are designed to fit around the back of your neck, with the cord coming out the back. So I decided to give them a try.

They worked out quite well. The ear piece doesn’t actually fit in your ear like earbuds; the band keeps the speakers pressed against, but not in, your ear. The band itself is plastic that is fexible; it keeps it’s shape, but can flex to shape to your head. The cord runs through a channel in the band. The connector worked fine with the Sansa Express. I used them for the first time on Friday, August 31, 2007. I plan to use them on my next run.