-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPizza.js
More file actions
91 lines (74 loc) · 2.62 KB
/
Pizza.js
File metadata and controls
91 lines (74 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// listen for button clicks
document.getElementById("placeOrder").addEventListener("click", placeOrder);
/**
* gets form values
* calculates prices
* produces output
*/
function placeOrder() {
// get form values
var numPizzas = document.getElementById("numPizzas").value;
var typePizza = document.getElementById("typePizza").value;
var deliveryCity = document.getElementById("deliveryCity").value;
var birthday = document.getElementById("birthday").value;
// get the pizza price
var orderPrice = calculatePrice(numPizzas, typePizza);
// get the delivery price
var deliveryPrice = calculateDelivery(orderPrice, deliveryCity, birthday);
// create the output
var theOutput = "<p>Thank you for your order.</p>";
// output the delivery price, if there is one
if (deliveryPrice === 0) {
theOutput += "<p>You get free delivery!</p>";
} else {
theOutput += "<p>Your delivery cost is: $" + deliveryPrice;
}
theOutput += "<p>Your total is: $" + (orderPrice + deliveryPrice);
// display the output
document.getElementById("displayTotal").innerHTML = theOutput;
}
/**
* calculates pizza price
*/
function calculatePrice(numPizzas, typePizza) {
var orderPrice = Number(numPizzas) * 10;
var extraCharge = 0;
// calculate extraCharge, if there is one
if (typePizza === "supreme") {
extraCharge = Number(numPizzas) * 2;
}
orderPrice += extraCharge;
return orderPrice;
}
/**
* calculates delivery price
*/
function calculateDelivery(orderPrice, deliveryCity, birthday) {
var deliveryPrice = 0;
// calculate delivery price, if there is one
if (((deliveryCity === "Anytown") && (orderPrice > 10)) || (birthday === "yes")) {
deliveryPrice = 0;
} else {
deliveryPrice = 5;
}
return deliveryPrice;
// listen for button clicks
document.getElementById("placeOrder").addEventListener("click", placeOrder);
/**
* gets form values
* calculates prices
* produces output
*/
function placeOrder() {
// Initialize orderPrice and deliveryPrice for the default message
var orderPrice = 600;
var deliveryPrice = 0;
// create the output
var theOutput = "<p>Thank you for your order. You get free delivery!</p>";
theOutput += "<p>Your total is: $" + (orderPrice + deliveryPrice) + "</p>";
// display the output
document.getElementById("displayTotal").innerHTML = theOutput;
}
// Rest of the code remains the same
// ...
}