Menu
Coddy logo textTech

상영 취소하기

Coddy JavaScript 여정의 논리 & 흐름 섹션에 포함된 레슨 — 65개 중 65번째.

challenge icon

챌린지

쉬움

상영 취소를 처리하는 cancelScreening 케이스를 추가하세요. data 매개변수는 다음을 포함합니다:

  • screeningId (number)

 

함수는 다음을 수행해야 합니다:

  1. festivalData.screenings에 주어진 screeningId를 가진 상영이 존재하지 않으면, resultsScreening not found!를 추가하고 중단합니다.
  2. festivalData.screenings 배열에서 해당 상영을 찾아 제거합니다.
  3. festivalData.tickets Set에서 관련된 모든 티켓을 제거합니다.
  4. results 배열에 Screening cancelled successfully!를 추가합니다.

직접 해보기

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;

            case "addScreening":
                const movie = festivalData.movies.find(m => m.id === currentData.movieId);
                const venue = festivalData.venues.find(v => v.id === currentData.venueId);
                
                if (!movie || !venue) {
                    results.push("Movie or venue not found!");
                    break;
                }
                
                const existingScreening = festivalData.screenings.find(s => 
                    s.venueId === currentData.venueId && 
                    s.date === currentData.date && 
                    s.time === currentData.time
                );
                
                if (existingScreening) {
                    results.push("Screening already exists at this time!");
                    break;
                }
                
                const newScreening = {
                    id: festivalData.screenings.length + 1,
                    movieId: currentData.movieId,
                    venueId: currentData.venueId,
                    date: currentData.date,
                    time: currentData.time,
                    availableSeats: venue.capacity
                };
                
                festivalData.screenings.push(newScreening);
                results.push("Screening added successfully!");
                break;

            case "buyTicket":
                const screening = festivalData.screenings.find(s => s.id === currentData.screeningId);
                
                if (!screening) {
                    results.push("Screening not found!");
                    break;
                }
                
                if (screening.availableSeats < currentData.quantity) {
                    results.push("Not enough seats available!");
                    break;
                }
                
                for (let i = 0; i < currentData.quantity; i++) {
                    const ticketId = `${currentData.screeningId}-${screening.availableSeats - i}`;
                    festivalData.tickets.add(ticketId);
                }
                
                screening.availableSeats -= currentData.quantity;
                results.push("Tickets purchased successfully!");
                break;


            case "rateMovie":
                const tempMovie = festivalData.movies.find(m => m.id === currentData.movieId);
                
                if (!tempMovie) {
                    results.push("Movie not found!");
                    break;
                }
                
                if (currentData.avgRating < 1 || currentData.avgRating > 5) {
                    results.push("Invalid rating! Must be between 1 and 5");
                    break;
                }

                tempMovie.avgRating = currentData.avgRating
                
                
                results.push("Rating added successfully!");
                break;

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

논리 & 흐름의 모든 레슨