Menu
Coddy logo textTech

Add Screening

Part of the Logic & Flow section of Coddy's JavaScript journey — lesson 62 of 65.

challenge icon

Challenge

Easy

Add a new test case to your switch statement: addScreening.

For addScreening the data parameters contains:

  • movieId (number)
  • venueId (number)
  • date (string in 'YYYY-MM-DD' format)
  • time (string in 'HH:MM:SS' format)

The function should:

  1. Validate if a movie with the given movieId and a venue with the given venueId exist in festivalData. If either is not found, add "Movie or venue not found!" to results
  2. Check if there's no screening at the same venue, date and time - If screening exists, add "Screening already exists at this time!" to results
  3. Create a new screening object with:
    • id: generate by adding 1 to the length of screening array
    • movieId, venueId, date, and time from the data
    • availableSeats: equal to venue's capacity
  4. Add the screening to festivalData.screenings array
  5. Add Screening added successfully! to results array

Try it yourself

const festivalData = {
	movies: [{
		id: 1,
		title: "Inception",
		director: "Christopher Nolan",
		year: 2010,
		mainGenre: "Sci-Fi",
		secondGenre: undefined,
		avgRating: 0,
		available: true
	}],
	venues: [{
		id: 1,
		name: "Main Theater",
		capacity: 200,
	}],
	screenings: [{
		id: 1,
		movieId: 1,
		venueId: 1,
		date: '2023-10-29',
		time: '13:35:00',
		availableSeats: 200
	}],
	tickets: new Set()
};

function manageFestival(actions, data) {
    let results = [];
    
    actions.forEach((action, index) => {
        const currentData = data[index];
        
        switch(action) {
            case "listMovies":
                results.push(festivalData.movies);
                break;
                
            case "listVenues":
                results.push(festivalData.venues);
                break;

            case "listTickets":
                results.push(festivalData.tickets);
                break;

            case "listScreenings":
                results.push(festivalData.screenings);
                break;

            case "addMovie":
                const newMovie = {
                    id: festivalData.movies.length + 1,
                    title: currentData.title,
                    director: currentData.director,
                    year: currentData.year,
                    mainGenre: currentData.mainGenre,
                    secondGenre: currentData.secondGenre,
                    avgRating: 0,
                    available: true
                };
                festivalData.movies.push(newMovie);
                results.push("Movie added successfully!");
                break;

            case "addVenue":
                const newVenue = {
                    id: festivalData.venues.length + 1,
                    name: currentData.name,
                    capacity: currentData.capacity,
                };
                festivalData.venues.push(newVenue);
                results.push("Venue added successfully!");
                break;


            default:
                results.push("Invalid action!");
        }
    });
    
    return results;
}

All lessons in Logic & Flow