Back to js-conditionals
05-library-card.js
JavaScript
1/**
2 * 📚 Maple Town Library
3 *
4 * The librarian at Maple Town Public Library is tired of manually checking
5 * whether members can borrow books. She asks you to automate it!
6 *
7 * A member can borrow books ONLY if ALL of these are true:
8 * 1. They are at least 6 years old
9 * 2. They have a valid library card (hasValidCard is true)
10 * 3. They have zero overdue books
11 *
12 * Return an object with two properties:
13 * - allowed: boolean (true if they can borrow, false otherwise)
14 * - message: string (a descriptive message)
15 *
16 * Check conditions in this order and return the FIRST failure:
17 * - Age < 6:
18 * { allowed: false, message: "Too young - must be at least 6 years old" }
19 *
20 * - No valid card:
21 * { allowed: false, message: "Invalid library card - please renew at the front desk" }
22 *
23 * - Has overdue books:
24 * { allowed: false, message: "Please return your X overdue book(s) first" }
25 * (replace X with the actual number of overdue books)
26 *
27 * - All conditions met:
28 * { allowed: true, message: "You may borrow up to 3 books" }
29 *
30 * @param {number} memberAge - The member's age
31 * @param {boolean} hasValidCard - Whether they have a valid library card
32 * @param {number} overdueBooks - Number of overdue books
33 * @returns {{ allowed: boolean, message: string }}
34 */
35export function canBorrowBook(memberAge, hasValidCard, overdueBooks) {
36 if(memberAge < 6){
37 return { allowed: false, message: "Too young - must be at least 6 years old" }
38 } else if(!hasValidCard){
39 return { allowed: false, message: "Invalid library card - please renew at the front desk" }
40 } else if(overdueBooks){
41 return { allowed: false, message: `Please return your ${overdueBooks} overdue book(s) first` }
42 }else {
43 return { allowed: true, message: "You may borrow up to 3 books" }
44 }
45}
46