Project: ticket price
JavaScript · Unit 2: Conditionals
- Full plan
- JavaScript
- Project
- 4 exercises
You can store the result in a variable and change it with another if before the return.
That way a second rule applies on top of the first one.
function total(price, member) {
if (member) {
price = price - 2;
}
return price;
}
console.log(total(15, true));
console.log(total(15, false));Prints
13 15
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Predict the output
What does this code print?
2. Write the code
At the movies, kids under 12 pay 9, people 60 or older pay 11 and everyone else pays 15. Write
ticketPrice(age), which hands back the price.3. Find the bug
Shipping is free on Sundays, and today is Monday. It should print 8. Which line has the error?
4. Write the code
On Tuesdays the ticket is half price for everyone. Write
ticketPrice(age, day), which works out the price by age as before and, ifdayis "Tuesday", divides it by 2.
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.