Use a double-subscripted array to solve the following problem. A company has four salespeople (1 to 4) who sell five dif- ferent products (1 to 5). Once a day, each salesperson passes in a slip for each different type of product actually sold. Each slip contains
1. the salesperson number,
2. the product number, and
3. the total dollar value of that product sold that day.
Thus, each salesperson passes in between 0 and 5 sales slips per day. Assume that the information from all of the slips for last
month is available. Write a script that will read all this information for last month’s sales and summarize the total sales by salesper-
son by product. All totals should be stored in the double-subscripted array sales. After processing all the information for last
month, display the results in an HTML table format with each of the columns representing a particular salesperson and each of the
rows representing a particular product. Cross total each row to get the total sales of each product for last month; cross total each
column to get the total sales by salesperson for last month. Your tabular printout should include these cross totals to the right of the
totaled rows and to the bottom of the totaled columns.
```
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2 <!-- ex17_17.html -->
3
4 <HTML>
5
6 <HEAD>
7 <TITLE>Solution: 17.17</TITLE>
8
9 <SCRIPT LANGUAGE = "JavaScript">
10 var person, product;
11 var sales = [ [ 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0 ], [ 0, 0, 0, 0, 0 ],
12 [ 0, 0, 0, 0, 0 ] ];
13 var totalPerson = [ 0, 0, 0, 0 ];
14 var totalProducts = [ 0, 0, 0, 0, 0 ];
15
16 function addToTotals()
17 {
18 var s = parseFloat( document.myForm.sales.value );
19 person = parseInt( document.myForm.salesPerson.value );
20 product = parseInt( document.myForm.product.value );
21
22 if ( person >= 1 && person < 5 && product >= 1 && product < 6 )
23 sales[ person - 1 ][ product - 1 ] += s;
24 else
25 window.alert( "Invalid input!" );
26
27 summary();
28 }
29
30 function summary()
31 {
32 var total = 0.0;
33
34 for ( var j = 0; j < totalProducts.length; ++j )
35 totalProducts[ j ] = 0;
36
37 var result = "Product\t\t1\t2\t3\t4\t5\tTotal";
38
39 for ( var row = 0; row < sales.length; ++row ) {
40 result += "\nSales person " + ( row + 1 );
41 total = 0.0;
42
43 for ( var col = 0; col < sales[ row ].length; ++col ) {
44 total += sales[ row ][ col ];
45 result += "\t" + sales[ row ][ col ];
46 totalProducts[ col ] += sales[ row ][ col ];
47 }
48
49 result += "\t" + total;
50 }
51
52 result += "\nTotal\t";
53
54 for ( var k = 0; k < totalProducts.length; ++k )
55 result += "\t" + totalProducts[ k ];
56
57 document.myForm.output.value = result;
58 }
59 </SCRIPT>
60 </HEAD>
61
62 <BODY>
63 <FORM NAME = "myForm">
64 <TABLE BORDER = "0">
65 <COLGROUP>
66 <COL ALIGN = "right">
67 </COLGROUP>
68
69 <TR><TD>Enter sales person number: </TD>
70 <TD><INPUT NAME = "salesPerson" TYPE = "text"></TD></TR>
71 <TR><TD>Enter product number:</TD>
72 <TD><INPUT NAME = "product" TYPE = "text"></TD></TR>
73 <TR><TD>Enter sales amount:</TD>
74 <TD><INPUT NAME = "sales" TYPE = "text"></TD></TR>
75 <TR><TD><INPUT TYPE = "button" VALUE = "Enter Sales Figure"
76 ONCLICK = "addToTotals()"</TD></TR>
77 <TR><TD COLSPAN = "2"><TEXTAREA NAME = "output" ROWS = "7" COLS = "70">
78 </TEXTAREA></TD></TR>
79 </TABLE>
80 </FORM>
81 </BODY>
82 </HTML>
```
You might also like to view...
?When practicing a presentation, write a script so that the presentation can be read to the audience.
Answer the following statement true (T) or false (F)
In _____, two programmers work on the same task on the same computer; one drives while the other navigates.
A. pair programming B. multitasking C. dual tasking D. driver programming
You can click the _____ button to undo an automatic correction.
A. Auto Fill Options B. AutoCorrect Options C. Paste Options D. Trace Error
The body of the order contains one or more order lines, sometimes called line ____________________.
Fill in the blank(s) with the appropriate word(s).