Assignment 6 -Intro to programming javascript
MIT 153 – Assignment #6
This assignment requires you to use a separate data file
names Asgn6Data.js.
This is an external JavaScript file can be “imported” into
your Web page by putting the following tags in the “head” section:
<script
type=”text/javascript”
src=”Asgn6Data.js”></script>
This file declares a number of arrays and fills them with
data. Open the file in a text editor and inspect the data. All of these
variables and values will now be available inside your page. (DO NOT try to
declare these variables in your code – once you have inserted the above tags
these variables are already a part of your page.)
Write a program that will use an array to accumulate the total
sales by department number as well as an overall total.
A sample output is shown below.
Note: the department numbers range from one to ten. Use an
array of size ten to hold the totals and display the names of the departments
from the departNames array provided in the file.
Sales by Department
Department | Total |
Housewares | 26.13 |
Kitchen | 12.66 |
Living Room | 15.63 |
Patio | 8.95 |
Garden | 17.25 |
Hardware | 14.73 |
Sporting Goods | 20.61 |
Automotive | 18.36 |
Tools | 204.67 |
Chemicals | 66.43 |
Total sales: 405.42
Answer
<!DOCTYPE html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="Asgn6Data.js"></script>
<title></title>
</head>
<body>
<script type="text/javascript" >
var total = new Array(10);
var salesTotal=0;
var BR="<br/>";
for (var idx = 1; idx <= 10; ++idx) {
total[idx] = 0;
}
for (var x = 0; x <40; ++x) {
var itm=itemDept[x];
total[itm] += itemQnty[x]*itemPrc[x];
}
document.write("<h2>Sales by Department</h2>");
document.write("<table width='200' border='1' ><tr><td><strong>Department</strong><td width='70' border='1'><strong>Total</strong></td></td></tr><table>");
for (var dx = 0; dx <10; ++dx) {
document.write("<table width='200' border='1' ><tr><td>"+departNames[dx] +" "+ "<td width='70' border='1'>"+(total[dx+1]).toFixed(2)+"</td></td></tr><table>");
salesTotal += total[dx+1];
}
document.write("<h4>"+BR+"Total Sales:"+" " +salesTotal +"</h4>");
</script>
</body>
</html>
Leave a reply