CSci 4657 Lab 3. Due Wednesday, February 9 at 11:59pm

Goals of this lab:

You may work alone or in pairs.

Lab tasks

Use javascript closures to implement a generator of Fibonacci numbers (15 points)

Fibonacci numbers are defined as follows: F0 = 0, F1 = 1, and Fk = Fk-1 + Fk-2 for all k >= 2.

Implement a javascript Fibonacci numbers using closures. Specifically, write an function that stores two consecuitive Fibonacci numbers, initially 0 and 1. The function also defines and returns a nested function getNext(). The getNext() function updates the two stored Fibonacci numbers to the next two Fibonacci numbers and returns the current one. E.g. on the first call to getNext() the return value is 0, on the next call it is 1, then 1 again, then 2, etc.

Create a form in the page that has a button and a text field. When the button is pressed, the text field displays the next Fibonacci number.

Use javascript closures to save and restore the state of a page (20 points)

Write a page that has a text area where the user can type in a story. Add a button to save the contents of the text area. When the user presses the button, the contents of the text area is stored into an local variable in a closure. The function generated by this closure will, when called, set the text area back to the saved text.

When a user presses the "save" button, a new button appears that shows the timestamp of the saved data. If the button is pressed, the function that was created for this save point is called, and the text is restored (overwriting the text in the text area). Your page should handle at least 4 of the "restore" buttons, reasonably arranged in the page.

Trying JSLint (10 points)

Run both of your solutions in JSLint. Fix issues that you think need fixing. Copy the JSLint report, remove duplicates, and explain why the remaining "errors" are OK to leave. If you don't quite understand an error message or what's causing it, just write a question about it. Submit your write-up as a text file.

What to submit.

Send me (CC your partner(s)) your .html and text files.


This page is a part of CSci 4657 course web site.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.