Need code for this javascript case question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • creative4u
    New Member
    • Jun 2010
    • 1

    Need code for this javascript case question

    I am taking a class to learn javascript, and this book has so many typo's, just typing in the code they suggest, I can never get this to work. I am NOT a programmer...bu t a 50 something year old person, wanting to learn more about web design. I applaud those who actually understand javascript. I think it is amazing how it can do so many things to a web page.

    If you can help, I certainly would appreciate it.

    Code:
    firstName = new Array();
    lastName = new Array();
    street = new Array();
    city = new Array();
    state= new Array();
    zip = new Array();
    amount = new Array();
    date = new Array()
    
    
    firstName[0]="Nina";
    lastName[0]="Largent";
    street[0]="88 Regal Lane";
    city[0]="Wheaton";
    state[0]="KY";
    zip[0]="89780";
    amount[0]=125;
    date[0]="2007-09-18";
    
    
    firstName[1]="Mike";
    lastName[1]="Hunt";
    street[1]="404 Barrow Street";
    city[1]="Drake";
    state[1]="KY";
    zip[1]="88841";
    amount[1]=75;
    date[1]="2007-09-18";
    
    
    firstName[2]="Monica";
    lastName[2]="Lang";
    street[2]="743 Stawlings Drive";
    city[2]="Youngston";
    state[2]="KY";
    zip[2]="89747";
    amount[2]=50;
    date[2]="2007-09-16";
    
    
    firstName[3]="William";
    lastName[3]="Mcknight";
    street[3]="102 Maple Lane";
    city[3]="Youngston";
    state[3]="KY";
    zip[3]="88660";
    amount[3]=150;
    date[3]="2007-09-15";
    
    
    firstName[4]="Latrina";
    lastName[4]="Hults";
    street[4]="750 Whitehall Road";
    city[4]="Drake";
    state[4]="KY";
    zip[4]="89664";
    amount[4]=250;
    date[4]="2007-09-14";
    
    
    firstName[5]="Danny";
    lastName[5]="Shamblin";
    street[5]="123 Smith Drive";
    city[5]="Delphi";
    state[5]="KY";
    zip[5]="88585";
    amount[5]=50;
    date[5]="2007-09-13";
    
    
    firstName[6]="Tina";
    lastName[6]="Ammons";
    street[6]="888 Evans Way";
    city[6]="Wheaton";
    state[6]="KY";
    zip[6]="89847";
    amount[6]=50;
    date[6]="2007-09-13";
    
    
    firstName[7]="Joanne";
    lastName[7]="Fine";
    street[7]="210 Bowling Terrace";
    city[7]="Wheaton";
    state[7]="KY";
    zip[7]="88632";
    amount[7]=125;
    date[7]="2007-09-11";
    
    
    firstName[8]="Charlotte";
    lastName[8]="Foulk";
    street[8]="109 South Road";
    city[8]="Youngston";
    state[8]="KY";
    zip[8]="88835";
    amount[8]=50;
    date[8]="2007-09-10";
    
    
    firstName[9]="Candice";
    lastName[9]="Alfaro";
    street[9]="108 Atwood Avenue";
    city[9]="Delphi";
    state[9]="KY";
    zip[9]="89611";
    amount[9]=400;
    date[9]="2007-09-08";
    
    
    firstName[10]="Kristi";
    lastName[10]="Laine";
    street[10]="512 North Lane";
    city[10]="Wheaton";
    state[10]="KY";
    zip[10]="88754";
    amount[10]=225;
    date[10]="2007-09-08";
    
    
    firstName[11]="Elisabeth";
    lastName[11]="Carbone";
    street[11]="381 Main Street";
    city[11]="Drake";
    state[11]="KY";
    zip[11]="89704";
    amount[11]=200;
    date[11]="2007-09-07";
    
    
    firstName[12]="James";
    lastName[12]="Larsen";
    street[12]="212 Rawlings Way";
    city[12]="Jasper";
    state[12]="KY";
    zip[12]="88646";
    amount[12]=125;
    date[12]="2007-09-07";
    
    
    firstName[13]="Ralph";
    lastName[13]="Thornton";
    street[13]="444 Smith Drive";
    city[13]="Delphi";
    state[13]="KY";
    zip[13]="89755";
    amount[13]=100;
    date[13]="2007-09-07";
    
    
    firstName[14]="Robin";
    lastName[14]="Witt";
    street[14]="78 Norland Pines";
    city[14]="Drake";
    state[14]="KY";
    zip[14]="88711";
    amount[14]=75;
    date[14]="2007-09-07";
    
    
    firstName[15]="Alex";
    lastName[15]="Ruiz";
    street[15]="102 Sunset Road";
    city[15]="Jasper";
    state[15]="KY";
    zip[15]="89614";
    amount[15]=50;
    date[15]="2007-09-06";
    
    
    firstName[16]="Callie";
    lastName[16]="Rudy";
    street[16]="3 Sunset Road";
    city[16]="Jasper";
    state[16]="KY";
    zip[16]="88576";
    amount[16]=50;
    date[16]="2007-09-06";
    
    
    firstName[17]="Michael";
    lastName[17]="Harrell";
    street[17]="125 Sunset Road";
    city[17]="Jasper";
    state[17]="KY";
    zip[17]="89711";
    amount[17]=50;
    date[17]="2007-09-06";
    
    
    firstName[18]="Edgar";
    lastName[18]="Morales";
    street[18]="387 North Lane";
    city[18]="Wheaton";
    state[18]="KY";
    zip[18]="89802";
    amount[18]=250;
    date[18]="2007-09-05";
    
    
    firstName[19]="Arlene";
    lastName[19]="Lutz";
    street[19]="7888 Clear View Drive";
    city[19]="Youngston";
    state[19]="KY";
    zip[19]="88699";
    amount[19]=75;
    date[19]="2007-09-05";
    
    
    firstName[20]="Earl";
    lastName[20]="Holmes";
    street[20]="1001 Rawlings Way";
    city[20]="Jasper";
    state[20]="KY";
    zip[20]="88781";
    amount[20]=500;
    date[20]="2007-09-04";
    
    
    firstName[21]="Bernice";
    lastName[21]="Drew";
    street[21]="25 Main Street";
    city[21]="Drake";
    state[21]="KY";
    zip[21]="88786";
    amount[21]=150;
    date[21]="2007-09-04";
    
    
    firstName[22]="Patrick";
    lastName[22]="Granier";
    street[22]="100 Atwood Avenue";
    city[22]="Delphi";
    state[22]="KY";
    zip[22]="88557";
    amount[22]=75;
    date[22]="2007-09-03";
    
    
    firstName[23]="Henry";
    lastName[23]="Bailey";
    street[23]="37 East Maple Street";
    city[23]="Youngston";
    state[23]="KY";
    zip[23]="89554";
    amount[23]=50;
    date[23]="2007-09-03";
    
    
    firstName[24]="Ginny";
    lastName[24]="Rainey";
    street[24]="657 Dawson Lane";
    city[24]="Youngston";
    state[24]="KY";
    zip[24]="88873";
    amount[24]=50;
    date[24]="2007-09-03";
    
    
    firstName[25]="Ginny";
    lastName[25]="Rainey";
    street[25]="657 Dawson Lane";
    city[25]="Youngston";
    state[25]="KY";
    zip[25]="88873";
    amount[25]=75;
    date[25]="2007-09-03";
    
    
    firstName[26]="Basilia";
    lastName[26]="Lu";
    street[26]="851 Flad Court";
    city[26]="Jasper";
    state[26]="KY";
    zip[26]="88633";
    amount[26]=500;
    date[26]="2007-09-02";
    
    
    firstName[27]="Livia";
    lastName[27]="Mckinnon";
    street[27]="557 Ivy Avenue";
    city[27]="Jasper";
    state[27]="KY";
    zip[27]="88960";
    amount[27]=50;
    date[27]="2007-08-31";
    
    
    firstName[28]="Kris";
    lastName[28]="Levesque";
    street[28]="542 Upton Avenue";
    city[28]="Delphi";
    state[28]="KY";
    zip[28]="88793";
    amount[28]=100;
    date[28]="2007-08-31";
    
    
    firstName[29]="Lynwood";
    lastName[29]="Ingersoll";
    street[29]="723 Jackson Avenue";
    city[29]="Delphi";
    state[29]="KY";
    zip[29]="88802";
    amount[29]=500;
    date[29]="2007-08-30";
    
    
    firstName[30]="Petronila";
    lastName[30]="Damico";
    street[30]="44 Stewart Street";
    city[30]="Drake";
    state[30]="KY";
    zip[30]="88604";
    amount[30]=250;
    date[30]="2007-08-30";
    
    
    firstName[31]="Hugh";
    lastName[31]="Warren";
    street[31]="585 Lindon Court";
    city[31]="Wheaton";
    state[31]="KY";
    zip[31]="88877";
    amount[31]=50;
    date[31]="2007-08-28";
    
    
    firstName[32]="Tom";
    lastName[32]="Thomas";
    street[32]="Rigel Avenue";
    city[32]="Drake";
    state[32]="KY";
    zip[32]="89411";
    amount[32]=100;
    date[32]="2007-08-27";
    
    
    firstName[33]="Steve";
    lastName[33]="Bones";
    street[33]="900 Lawton Street";
    city[33]="Wheaton";
    state[33]="KY";
    zip[33]="89211";
    amount[33]=50;
    date[33]="2007-08-25";
    
    
    firstName[34]="Jeri";
    lastName[34]="White";
    street[34]="Hawkes Lane";
    city[34]="Delphi";
    state[34]="KY";
    zip[34]="89211";
    amount[34]=150;
    date[34]="2007-08-25";

    1. suppose to add a script element that points to the list.js file
    2. Under this element insert another script element that contains the function amountTotal(). The purpose of this is to return the sum of all of the values in the amount array. There are no parameters for this fuction. Then add the following commands to the function: Declare a variable named total, setting its initial value to 0. Create a for loop that loops through all of the values in the amount array. At each iteration of the loop add the current value of the array item to the value of the total variable. Then after the for loop is completed, return the value of the total variable.
    3. locate the div element with the id "data_list" Within the div element add a script element that contains the following commands:

    a. write the following code to the document:
    Code:
    <table border='1' rules='rows' cellspacing='1'> 
    <tr>
     <th>Date</th>
     <th>Amount</th>
     <th>First Name</th>
     <th>Last Name</th>
     <th>Address</th> 
    </tr>
    B. Create a for loop in which the counter variable starts at 0 and while the counter is less than the length of the amount array increase the counter in increments of 1.

    C. Every other row in the data list is to be displayed with a yellow background; to do this, within the for loop insert an IF condition that tests whether the counter variable is divisible evenly by 2 using the % modulus operator. If the counter variable is divisible by 2 write the following HTML tag: <tr> Otherwise write the tag, <tr class='yellowro w'>

    D. next within the loop write the following html code to the document:
    Code:
    <td>date</td>
    <td class='amt'>amount</td>
    <td>firstName</td>
    <td>lastName</td>
    where date, amount, firstName and lastName are the values of the date, amuont, firstName, lastName arrays fro the index indicated by the current value of the for loop's counter variable.

    E. Finally within the for loop, write the html code
    Code:
    <td>street<br/>
    city, state zip
    </td>
    </tr>
    where street, city, state and zip are the values of the street, city state and zip arrays for the current index value.

    4. go to the div element with the id "totals" insert a script element that writes the following html code to the document.
    Code:
    <table border='1' cellspacing='1'>
    <tr>
    <th id='sumTitle' colspan='2'>
    summary
    </th>
    </tr>
    <tr>
    <th>Contributors</th>
    <td>contributions</td>
    </tr>
    <tr>
    <th>Amount</th>
    <td>$total</td>
    </tr>
    </table>
    where contributions is the length of the amount array and total is the value returned by the amountTotal() function you created earlier.

    Supposedly when complete, a list of 35 contributions totalling $5175 is displayed on the table and that alternate rows of the contributor list are displayed with a yellow background.
    Last edited by Dormilich; Jun 30 '10, 06:08 AM. Reason: Please use [code] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    as a bit more experienced programmer, I immediately see that this task is screaming for an object oriented solution. however, I try my best to explain it without it, since object oriented programming takes its time to understand.

    preface: if you ever want to do something in JavaScript, you must learn the basic syntax by heart (how do loops, conditions and functions work and how are they coded (in general)). this is true for every programming language. imagine it as the grammar. without it, you can’t talk sensibly.

    for starters, I recommend the w3schools tutorial. check also our offsite links.

    for now I’m just adding some comments

    0.) defining the data can be eased considerably:
    Code:
    // always declare your variables!
    // [] is shorthand for new Array(), you can immediately pass the elements to it.
    var firstname = ["Nina", "Mike", "Monica"]; // etc.
    3a.) as the task is given, it looks like document.write( ) is the desired answer. however, document.write( ) has various disadvantages, which limits its use.

    3b. - 4.) this is pretty much describing verbally what you have to type. if you manage 3a, this is not a problem.

    Comment

    Working...