Categories/sandwiches

From Unhosted Web Community Group
  • given (in javascript):
 var now; //an epoch timestamp, at which the user last changed their favourite sandwich
 var favSandwichIngr1, favSandwichIngr2; //two strings, being the two ingredients of the user's current favourite sandwich
 var storage; //an interface to the sandwiches category that behaves according to the WebStorage IDL.
   //if an item already exists on there, then it should be a JSON representation of a hash table.
  • execute the following javascript:
 //store the version:
 var str = JSON.stringify({
   ingredients: [favSandwichIngr1, favSandwichIngr2]
 });
 storage.setItem('favSandwich'+':'+now, str);
 //atomically set it to be the latest version:
 var index = JSON.parse(storage.getItem("index"));
 if(!index) {
   index = {};
 }
 latest["favSandwich"] = now;
 storage.setItem("index", JSON.stringify(index));