Question

Fruit Delivery Service Example TABLE ITEM-ORDERS Index I (ID Name Apples ABC DEF Mangos Melons Berries Oranges Pears 0 10 0 1

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here, we shall represent the table is 2D array, and use the indices to access the array. Each customer and his orders are looped through in order to get individual totals and grand total.

Code is a HTML page with JavaScript embedded

-- code start : answer.html --

<html>
<head>
<script type="text/javascript">
        // Table item-prices
        let items = [
            ["Apple",2],
            ["Orranges",1.5],
            ["Pears",2.5],
            ["Mangos",0.5],
            ["Mellons",2.0],
            ["Berries",3]
        ];
        // Table item-orders
        let orders = [
            ["ABC",5,4,2,10,2,0],
            ["DEF",8,3,4,0,0,2],
            ["XYZ",2,3,0,0,0,1],
            ["WXY",0,0,4,12,2,4],
            ["PQR",1,3,2,6,0,3]
        ];
        // Compute the total for each customer
        let grandTotal = 0;
        document.write("<h2> Customer reports </h2>");
        for (let i=0; i < orders.length; i++) {     // Loop through orders
            let customerTotal = 0;
            let order = orders[i];                    // Current order
            document.write("<table border=1><tr><td colspan=4> Customer Name: " + order[0] + "</td></tr>");
            document.write("<tr><td>Item</td><td>Quantity</td><td>Rate</td><td>Total</td></tr>");
            for (let j = 0; j < items.length; j++) { //Loop through items
                if (order[j+1] > 0) {                 // Check if current item is brought
                    let item = items[j];
                    let total = item[1] * order[j+1];
                    customerTotal += total;
                    document.write("<tr> <td>" + item[0] + "</td><td>" + order[j+1] + "</td><td>" +item[1] + "</td><td>" + total + "</td></tr>")  // Write table row
                }
            }
            customerTotal += 5;  // Delivery charge
            document.write("<tr><td colspan=3>Delivery charge</td><td>5</td></tr>");
            document.write("<tr><td colspan=3>Customer Total</td><td>" + customerTotal + "</td></tr></table>");
            grandTotal += customerTotal;
            document.write("<br/><br/>") // Spacing
        }
        document.write("<h2> Summary </h2>");
        document.write("Grand Total of a day = " + grandTotal);
    </script>
</head>
<body>

</body>
</html>

-- code end --

Sample output:

Customer reports Customer Name: ABC Item Quantity Rate Total Apple 5 2 10 1.5 llo Orranges 4 Pears2 2.5 5 Mangos 10 0.5 5 Mel

Delivery charge Customer Total 16.5 Customer Name: WXY Item Quantity Rate Total 2.5 10 Pears 4 Mangos 12 0.5 Mellons2 2 4 Ber

Add a comment
Know the answer?
Add Answer to:
Fruit Delivery Service Example TABLE ITEM-ORDERS Index I (ID Name Apples ABC DEF Mangos Melons Berries Oranges Pears 0...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT