Step 4: Adding Products to the Shopping Bag

Putting things in the shopping bag is easy. Users need only click the affectionately titled link “Gimme One.” This calls the correspondingly named function gimmeOne(). Lines 112-133 have the details:

function gimmeOne() {
  if (!gimmeControl) {
     alert("Nothing on this screen to give you.");
  for (var i = 0; i < shoppingBag.things.length; i++) {
     if (categorySet[curCLoc].prodLine[curPLoc].plu ==
       shoppingBag.things[i].plu) {
      alert("That's already in your bag. You can change the quantity " +
         "by choosing View/Change Bag.");
  shoppingBag.things[shoppingBag.things.length] =
  shoppingBag.things[shoppingBag.things.length - 1].itemQty = 1;
  shoppingBag.things[shoppingBag.things.length - 1].category =
  alert("OK. You put the " +
     shoppingBag.things[shoppingBag.things.length - 1].name +
     " in your bag.");


The first thing gimmeOne() does is to ensure that there is actually something on the screen to put in the shopping bag. Variable gimmeControl is set to true immediately before a product is displayed. Otherwise, any other functions displaying information on the screen set gimmeControl to false. Therefore, if gimmeControl is false, there is no product on the screen. The user is alerted, and gimmeOne() returns. Otherwise, gimmeOne() iterates through the elements of the things array, which is a property of the user’s shoppingBag object to check whether ...

Get JavaScript Application Cookbook now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.