|
| 1 | +<!doctype html> |
| 2 | +<html lang="en"> |
| 3 | +<head> |
| 4 | +<meta charset="utf-8"> |
| 5 | +<title>First class functions</title> |
| 6 | +<script> |
| 7 | + |
| 8 | +var passengers = [ { name: "Jane Doloop", paid: true, ticket: "coach" }, |
| 9 | + { name: "Dr. Evel", paid: true, ticket: "firstclass" }, |
| 10 | + { name: "Sue Property", paid: false, ticket: "firstclass" }, |
| 11 | + { name: "John Funcall", paid: true, ticket: "premium" } ]; |
| 12 | + |
| 13 | +function processPassengers(passengers, test) { |
| 14 | + for (var i = 0; i < passengers.length; i++) { |
| 15 | + if (test(passengers[i])) { |
| 16 | + return false; |
| 17 | + } |
| 18 | + } |
| 19 | + return true; |
| 20 | +} |
| 21 | + |
| 22 | +function checkNoFlyList(passenger) { |
| 23 | + return (passenger.name === "Dr. Evel"); |
| 24 | +} |
| 25 | + |
| 26 | +function checkNotPaid(passenger) { |
| 27 | + return (!passenger.paid); |
| 28 | +} |
| 29 | + |
| 30 | +function printPassenger(passenger) { |
| 31 | + var message = passenger.name; |
| 32 | + if (passenger.paid === true) { |
| 33 | + message = message + " has paid"; |
| 34 | + } else { |
| 35 | + message = message + " has not paid"; |
| 36 | + } |
| 37 | + console.log(message); |
| 38 | + return false; |
| 39 | +} |
| 40 | + |
| 41 | +// |
| 42 | +// plane can only fly if every passenger is on the fly flist |
| 43 | +// |
| 44 | +var allCanFly = processPassengers(passengers, checkNoFlyList); |
| 45 | +if (!allCanFly) { |
| 46 | + console.log("The plane can't take off: we have a passenger on the no fly list."); |
| 47 | +} |
| 48 | + |
| 49 | +// |
| 50 | +// plane can only fly if every passenger has paid |
| 51 | +// |
| 52 | +var allPaid = processPassengers(passengers, checkNotPaid); |
| 53 | +if (!allPaid) { |
| 54 | + console.log("The plane can't take off: not everyone has paid."); |
| 55 | +} |
| 56 | + |
| 57 | +// |
| 58 | +// we don't care about the result here; we're just using |
| 59 | +// processPassengers to display the passenger list |
| 60 | +// |
| 61 | +processPassengers(passengers, printPassenger); |
| 62 | + |
| 63 | +function createDrinkOrder(passenger) { |
| 64 | + var orderFunction; |
| 65 | + if (passenger.ticket === "firstclass") { |
| 66 | + orderFunction = function() { |
| 67 | + alert("Would you like a cocktail or wine?"); |
| 68 | + }; |
| 69 | + } else if (passenger.ticket === "premium") { |
| 70 | + orderFunction = function() { |
| 71 | + alert("Would you like wine, cola or water?"); |
| 72 | + }; |
| 73 | + } else { |
| 74 | + orderFunction = function() { |
| 75 | + alert("Your choice is cola or water."); |
| 76 | + }; |
| 77 | + } |
| 78 | + return orderFunction; |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +function createDinnerOrder(passenger) { |
| 83 | + var orderFunction; |
| 84 | + if (passenger.ticket === "firstclass") { |
| 85 | + orderFunction = function() { |
| 86 | + alert("Would you like chicken or pasta?"); |
| 87 | + }; |
| 88 | + } else if (passenger.ticket === "premium") { |
| 89 | + orderFunction = function() { |
| 90 | + alert("Would you like a snack box or cheese plate?"); |
| 91 | + }; |
| 92 | + } else { |
| 93 | + orderFunction = function() { |
| 94 | + alert("Would you like peanuts or pretzels?"); |
| 95 | + }; |
| 96 | + } |
| 97 | + return orderFunction; |
| 98 | +} |
| 99 | + |
| 100 | +function pickupTrash() { |
| 101 | + alert("Can I have your trash, please?"); |
| 102 | +} |
| 103 | + |
| 104 | +function serveCustomer(passenger) { |
| 105 | + var getDrinkOrderFunction = createDrinkOrder(passenger); |
| 106 | + var getDinnerOrderFunction = createDinnerOrder(passenger); |
| 107 | + |
| 108 | + getDrinkOrderFunction(); |
| 109 | + |
| 110 | + // get dinner order |
| 111 | + getDinnerOrderFunction(); |
| 112 | + |
| 113 | + getDrinkOrderFunction(); |
| 114 | + getDrinkOrderFunction(); |
| 115 | + |
| 116 | + // show movie |
| 117 | + |
| 118 | + getDrinkOrderFunction(); |
| 119 | + |
| 120 | + // pick up trash |
| 121 | + pickupTrash(); |
| 122 | +} |
| 123 | + |
| 124 | +function servePassengers(passengers) { |
| 125 | + for (var i = 0; i < passengers.length; i++) { |
| 126 | + serveCustomer(passengers[i]); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +servePassengers(passengers); |
| 131 | + |
| 132 | +</script> |
| 133 | +</head> |
| 134 | +<body> |
| 135 | +</body> |
| 136 | +</html> |
| 137 | + |
0 commit comments