Work out the price of a cinema ticket from several rules.
What to build
A program in which each rule is a separate, simple step.
Requirements
Apply these rules, in this order:
- A person under 5 pays nothing: print
Freeand do nothing else. - The base price is 100 for ages 5 to 12, 200 for ages 13 to 59, and 120 for ages 60 and over.
- A student aged 13 to 59 gets 40 off. The student answer is ignored for other ages.
- On
FriorSat, add 30.
Otherwise print Price: N taka.
Input
Three lines: the age, the day (Mon, Tue, Wed, Thu, Fri, Sat or Sun) and yes or no for student.
Output
Free, or Price: N taka.
Constraints
The age is a whole number from 0 to 120.
Example
Input:
20
Fri
yes
Output:
Price: 190 taka
Why it works
Keeping one running price and changing it step by step is far easier to check than one huge condition for every combination. The early test handles the case to which no other rule applies.
Examples
Example 1 · Input
30 Mon no
Expected output
Price: 200 taka
Example 2 · Input
20 Fri yes
Expected output
Price: 190 taka
Hints
Hint 1
Deal with the "Free" case first and stop there.
Hint 2
Work out the base price with an if chain on the age.
Hint 3
Apply the student discount, then the weekend charge, one step at a time.
Solution
Show one correct solution (try it yourself first)
const age = Number(prompt());
const day = prompt();
const student = prompt();
if (age < 5) {
console.log("Free");
} else {
let price;
if (age <= 12) {
price = 100;
} else if (age <= 59) {
price = 200;
} else {
price = 120;
}
if (student === "yes" && age >= 13 && age <= 59) {
price -= 40;
}
if (day === "Fri" || day === "Sat") {
price += 30;
}
console.log(`Price: ${price} taka`);
}