Buy Tickets
Part of the Logic & Flow section of Coddy's JavaScript journey — lesson 63 of 65.
Challenge
EasyAdd case buyTicket that handles ticket purchases. The data parameter contains:
- screeningId (number)
- quantity (number)
The function should:
- Check if screening exists and has enough available seats
- If not found, add
Screening not found!to results array - If not enough seats, add
Not enough seats available!to results array
- If not found, add
- Create ticket IDs following this exact format:
- For each ticket, create ID as:
${screeningId}-${currentAvailableSeats} - Example: If screeningId is 1 and availableSeats is 200, and quantity is 2:
- First ticket ID will be "1-200"
- Second ticket ID will be "1-199"
- For each ticket, create ID as:
- Add each ticket ID to
festivalData.ticketsSet - Subtract quantity from screening's availableSeats
- Add
Tickets purchased successfully!toresultsarray
Try it yourself
// Don't remove the lines below
const util = require('util');
util.inspect.defaultOptions.maxArrayLength = null;
// ---------------------------
All lessons in Logic & Flow
1Strings In Depth
String FundamentalsIterate Over StringsTemplate LiteralsString MethodsRecap - String Weaver4JSON Part 2
Iterate Over JSONNested JSONJSON Optional ChainingShallow And Deep CopyRecap - Bicycle ShopRecap - Solar System10Manage Festival System
Project OverviewAdd Movies & Venues2Multi-dimensional Arrays
2D Arrays BasicsAccessing 2D Array ElementsNested Loops with 2D ArraysRecap - 2D ArraysMatrix Addition & SubstractionJagged Arrays3D Arrays And BeyondCommon 2D Array PatternsRecap - All About Arrays5Sets Part 1
What Is A Set?Iterating Over SetsAdding An ElementRemoving An ElementChecking If An Element ExistsSize And Is EmptyCopy And ClearRecap - Basic Of Sets8Arrays Interesting Topics
Array DestructuringSpread Syntax in ArraysSparse ArraysRecap - Arrays Workshop3JSON Part 1
What is a JSON?Check If Key ExistsObject MethodsThe Spread Operator Part 1The Spread Operator Part 2Remove KeysRecap - JSON Manipulate Keys