From nmiller@channel1.com Tue Dec 1 15:44:36 1998 Date: Sun, 29 Nov 1998 17:05:07 -0500
From: Neil Miller <nmiller@channel1.com> To: rich@on-the-net.com
Subject: NEU Javascript Assignment

Rich - here is my work for Assignment One of the JavaScript class. I was trying to keep it simple, but the function seemed to get more complex the more I thought about it. I attempted to do some actual coding, but haven't been able to actually pull it off yet...

Hope your holiday was happy.

Neil

I would like to implement a "shopping basket" application to keep track of customer orders in an online catalog. As customers browse through the text and graphics of a catalog, they should be able to easily add items to their "baskets." The application would:

  1. Add the selected item to the shopping basket list.
  2. Display contents of shopping basket to the visitor at "check out" time.

The logic of the application is as follows:

Step 1- Receive item attributes (catalog #, item name, price) from hyperlink that calls the function.

1.1 Assign passed information to variable.

Step 2- Check for existing items in cookie file; create new cookie if none exists.

2.1 Extract existing items from cookie string and separate them from cookie name.

Step 3 - Add item to cookie file.

3.1 Add new item variable to existing shopping basket string. Separate items and item attributes with delimiters so string can be parsed at check out.

3.2 Assign new shopping basket string to cookie name.

Step 4 Repeat steps 1-3 for each item visitor selects for purchase.

Step 5 At check out, parse cookie string at delimiters to get separate items.

5.1 Dynamically build form that displays each item's catalog #, name and price.

This application would be done with 4 functions:

shoppingBasket() - adds information to cookie.

splitCookie() - splits cookie into separate items

splitGift() - splits each item into its attributes (catalog#, name and price).

createForm() - builds form to display items & attributes.

Attempts at code:

function shoppingBasket(itemCatalog, itemName, itemPrice) { item=itemCatalog + ":" + itemPrice + ":" + itemName + "||" //combine attributes into one delimited string if (document.cookie !="") {

var list=document.cookie.split("=")[1] //variable list=string of existing items
document.cookie="basket=" + list + item //combine new item w/old & assign to cookie

}else{
document.cookie="basket=" + item //if cookie value is null then create cookie w/new item
}
}

function splitCookie() {
if (document.cookie !="") { //if cookie file is not a null value var list=document.cookie.split("=")[1] //variable list=string of existing items
var gifts= new Array ()
var i=0
nextIndex=0

     while (nextIndex<list.length) {
       var  j= list.indexOf("||", nextIndex) //split cookie string by
finding delimiter "||"
         gifts[i] = list.substring(nextIndex,j)  //assign substring

between dilimiters to array

nextIndex=j+1 //increase nextIndex to start next delimiter search

         i++
        }

splitGift(gifts) //send gifts array to be split into characteristics of each gift

}else{ //if cookie file is null

        alert("You have no items in your shopping basket."
       document.location.href="catalogpage1.html"
      }

}

function splitGift(gifts)
var catalog= new Array() //create arrays for 3 gift characteristics

       var giftname = new Array()
       var giftprice = new Array()
       characCount=0
       nextIndex=0
            for (i=0, i<=gifts.length, i++) {
               var j=gifts[i].indexOf(":",nextIndex)      //split string
by finding delimiter ":"
                 if (i==0) {          //if first time through loop then
string=catalog#
                 catalog[i]=gifts[i].substring(nextIndex,j)
                   }
               if (characCount==1) {  //if 2nd time through, then
string=gift name
                 giftname[i]=gifts[i].substring(nextIndex,j)
                         }else{
                giftprice[i]=gifts[i].substring(nextIndex,j) // if 3rd
time, then string=price
                         }
           nextIndex=j + 1
           charCount=characCount++
            }
         createForm(catalog, giftname, giftprice) //pass info to
createForm function
      }

function createForm() {
for (i=0, i<=catalog.length, i++) {
var line="<input='text' name=' "+ catalog[i] + "' size=10 value="+ catalog[i] + ">"
line+="<input='text' name =' " + giftname[i] + "' size=20 value=" + giftname[i] + ">"
line+="<input='text' name=' " + giftprice[i] + "' size=7 value=$"+ giftprice[i] + ">"

      }
            //cycle through arrays & assign values to form values on

page
}