FunctionsProLesson 4 of 45, about 5 min
- 1
- 2
- 3
- 4
- 5
- 6
The app: QuickBite
QuickBiteYour order
Bangkok Garden, Thai25–35 min
Pad thaiRice noodles, tamarind, peanuts. $11.50 each
2
Spring rollsCrispy, with sweet chili sauce. $6.50 each
1
2× Pad thai$23.00
1× Spring rolls$6.50
Delivery$2.99
Total$32.49
Deliver to 12 Elm Street
The data behind it
cart the data
[
{
"dish": ,
"price": ,
"qty":
},
{
"dish": ,
"price": ,
"qty":
}
]
cart.js two functions
1function changeQty(item, amount) {2 item.qty = item.qty + amount;3}4 5function lineTotal(price, qty) {6 return price * qty;7}8 9const total = lineTotal(cart[0].price, cart[0].qty)10 + lineTotal(cart[1].price, cart[1].qty) + 2.99;
Calls made when the page opened
lineTotal(, ) →
lineTotal(, ) →
The MagisterOn the left, a QuickBite order. On the right, the code behind it. The + button has no logic of its own: it calls changeQty, a function, which is a named set of steps the app can run whenever it needs. Tap + on Pad thai.
Step 1 of 6Tap + on Pad thai
- Pad thai is at 3
You’re not signed in, so this lesson won’t be saved to your path. Sign in free to keep your XP.