1. Tech

Your suggestion is on its way!

An email with a link to:

http://javascript.about.com/library/bleaster1.htm

was emailed to:

Thanks for sharing About.com with others!

Unobtrusive Easter Calculator

1. Introduction

One area where JavaScript excels is in being able to add interactive calculators into your web page where your visitors can enter some information, press a button, and be presented with an answer.

Let's look at the code needed to create a calculator that does something a little more significant than just allowing our visitors to add or multiply two numbers together. What we are going to do is to create an Easter Day calculator where your visitors will enter the year and the calculator will tell them the day and month that Easter Day falls on in that year.

To be able to create an Easter calculator we first need to know how Easter is calculated. I found the following formula in a book called "The Calendar" by David Ewing Duncan. This particular formula has appeared in many other places before appearing in that book and has also appeared in lots of other places since.

This particular formula has advantage over the alternatives in that no table lookups or adjustments are required. It is also rather convenient because the way that the formula is set out in the book is almost exactly the way that we need it for our JavaScript.

In a real world situation we'll want to set the calculator up to use a server side scripting language to do the calculation for when JavaScript is disabled. We'll ignore this consideration for the moment in order to concentrate on the JavaScript calculations.

Here's the code as it appears in the book.

a = year % 19
b = year / 100
c = year % 100
d = b / 4
e = b % 4
f = (b + 8) / 25
g = (b - f + 1) / 3
h = (19 * a + b - d -g + 15) % 30
i = c / 4
k = c % 4
l = (32 + 2 * e + 2 * i - h - k) % 7
m = (a + 11 * h + 22 * l) / 451
Easter Month = (h + l - 7 * m + 114) / 31 [3 = March, 4 = April]
p = (h + l - 7 * m + 114) % 31
Easter date = p + 1

2. Converting to JavaScript

See More About

©2014 About.com. All rights reserved.