Back to js-conditionals
01-ticket-pricing.js
JavaScript
1/**
2 * 🎬 The Starlight Cinema
3 *
4 * You've just been hired at Starlight Cinema! Your first task is to build
5 * the automated ticket pricing system. The manager hands you a sticky note
6 * with the pricing rules scribbled on it:
7 *
8 * Age Groups:
9 * - Children (0–12): $8
10 * - Teens (13–17): $12
11 * - Adults (18–59): $15
12 * - Seniors (60+): $10
13 *
14 * Weekend Surcharge:
15 * - Add $3 on weekends (when isWeekend is true)
16 *
17 * Rules:
18 * - If age is negative or not a number, return -1
19 * - isWeekend is a boolean
20 *
21 * @param {number} age - The customer's age
22 * @param {boolean} isWeekend - Whether it's a weekend
23 * @returns {number} The ticket price, or -1 for invalid input
24 */
25export function getTicketPrice(age, isWeekend) {
26
27 if (age < 0 || typeof age !== "number" ) {
28 return -1;
29 }
30
31 let price;
32
33 if (age <= 12) {
34 price = 8;
35 } else if (age <= 17) {
36 price = 12;
37 } else if (age <= 59) {
38 price = 15;
39 } else {
40 price = 10;
41 }
42
43 if (isWeekend === true) {
44 price += 3;
45 }
46
47 return price;
48}
49
50