/jse-fall15-5

JS-evening fall 2015 Homework, week 5

Primary LanguageJavaScriptMIT LicenseMIT

Homework 5

Due Mon. Aug.17

####Synopsis

  • Problem 1: A Cards Module [20% of total time] Goals: Start working with IIFEs and closure to create a self-contained module for your cards.
  • Problem 2: All Hands Off Deque [25%] Goals: Get more comfortable with closure by creating a secure version of the deque from last week.
  • Problem 3: Secrets At All Levels [25%] Goals: Build on Monday's in-class password example to practice closures at the level of both instance and factory methods.
  • Problem 4: Cancelled! Enjoy Javascript through other means!


1) Card module

Package your earlier playing-card code into a module; that is, wrapped inside an immediately-invoked function expression (IIFE, or "Iffy"). Your module should return one object: the factory makeCard. As before, calling makeCard(id) should create and return a card object with methods for rank, suit, name, etc. But this time, the shared methods don't need to be linked initially to the factory; they can just be ordinary functions within the IIFE, where they are protected from the global scope.

The instance methods still need to be linked to each instance, and the factory methods (e.g. isCard()) still needs to be linked the factory. But any other helper-functions or arrays which do not need to be public should remain inaccessible from outside the IIFE.

You may use the template file to get started.


2) All hands off deque

The implementation of a deque in Homework 4, Problem 2e), tries to maintain the integrity of the deque contents by preventing a push or unshift of items not in the original deque. But a programmer could deliberately or accidentally circumvent those efforts by accessing and changing the deque's array instead of using its methods.

a) Write another version of a deque factory which protects the deque instances by using closure to hide their content arrays from the outside world. Your deque methods should be the only way of changing their hidden arrays. You may use the template file to get started.

(Hint #1: you'll have to give up the strategy of sharing factory methods with instances to avoid redundancy. Instead, have each call to the factory generate a set of methods specific to one deque instance which can access any private arrays associated with it.)

(Hint #2: the private arrays will live in a function scope, not in an object.)

b) Wrap the deque factory in an IIFE to create a module which exports makeDeque.


3) Secrets at all levels

a) Write a user-registration tool, a factory function makeUser(name,pwd) which accepts a username and password and generates a user object. Once we have a user object we should be able to do two things with it: retrieve the corresponding username and test to see if a provided password matches that user's password. Each user will have these methods:

  • getName() returns the username;
  • validate(str) takes a string and returns true if it matches that user's password.

It should not be possible, however, to modify the username or password once created nor to directly see the password.

Here is a template to get you started.

b) Now that we can make user objects, let's assume that our system needs some version of a "system log" that will record messages left by different users. This system log, being shared by all user objects created, will contain all the messages that users have recorded. You will need to modify the factory you made above to be a part of a module that has a private variable that holds the system log.

  • Each user object should have an additional method record(message) which writes an entry to the shared log in the format "username: message" and returns true. If no message is provided, the record method should return undefined instead.

  • Reading from the log is a operation of the system and not of individual users. The factory itself should have a method getLog(username) whose argument username is optional. If username is provided, getLog should return a string of all log entries recorded by that user. If username is omitted (therefore undefined), return a string of all log entries from everyone. In either case, log entries should be separated by newlines.

The log should not be able to be modified other than through a user's record method.


4)

Cancelled. Its treasures will appear on another homework set someday.