上映の追加
CoddyのJavaScriptジャーニー「論理とフロー」セクションの一部 — レッスン 62/65。
チャレンジ
簡単switch文に新しいテストケース addScreening を追加してください。
addScreening の場合、データパラメータには以下が含まれます:
- movieId (数値)
- venueId (数値)
- date ('YYYY-MM-DD' 形式の文字列)
- time ('HH:MM:SS' 形式の文字列)
関数は以下の処理を行う必要があります:
- 映画と会場が存在するか検証する - 映画または会場が見つからない場合は、results に "Movie or venue not found!" を追加します。
- 同じ会場、日付、時間に上映予定がないか確認する - 上映予定が既に存在する場合は、results に "Screening already exists at this time!" を追加します。
- 以下の内容で新しい上映オブジェクトを作成する:
- id: screening 配列の長さに 1 を加えて生成
- data から取得した movieId, venueId, date, time
- availableSeats: 会場の収容人数(capacity)と同じ値
- 作成した上映オブジェクトを
festivalData.screenings配列に追加する - results 配列に
Screening added 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;
default:
results.push("Invalid action!");
}
});
return results;
}