settingsAccountsettings
Menusettings

Q: Movie Prices (JavaScript Task)

+2 votes

Movie ticket prices in a big retro-cinema depend on the movie title and on the day of week as shown below:

table of movie prices by days

Write a JavaScript function that calculate the ticket price by movie title and day of week.

The input comes as array of 2 strings:

  • The first string holds the movie title
  • The second string holds the day of week

Examples:

Input:
The Godfather
Friday

Output:
15


Input:
casablanca
sunday

Output:
10


Input:
Schindler's LIST
monday

Output:
8.50


Input:
SomoMOvie
Ninedays

Output:
error

The output should hold the ticket price or “error” if the title or day of week is invalid.

asked in JavaScript category by user nikole

1 Answer

+1 vote

My code and solution to this interesting movie JavaScript task with some explanations:

  • Turn all input values to lowercase to avoid character casing mistakes.
  • Check the movie title in if-else-if statement.
  • For each movie title check the price in a switch-case.
  • Beware of invalid movie titles and invalid days of week. Show “error” in such cases.
     
function movieDay(arrStr) {
    let movie = arrStr[0].toLowerCase();
    let day = arrStr[1].toLowerCase();

    let price = 0;
    if (movie == "the godfather") {
        switch (day) {
            case "monday":
                console.log(price += 12);
                break;
            case "tuesday":
                console.log(price += 10);
                break;
            case "wednesday":
                console.log(price += 15);
                break;
            case "thursday":
                console.log(price += 12.50);
                break;
            case "friday":
                console.log(price += 15);
                break;
            case "saturday":
                console.log(price += 25);
                break;
            case "sunday":
                console.log(price += 30);
                break;
            default:
                console.log("error");
                break;
        }
    }
    if (movie == "schindler's list") {
        switch (day) {
            case "monday":
                console.log(price += 8.50);
                break;
            case "tuesday":
                console.log(price += 8.50);
                break;
            case "wednesday":
                console.log(price += 8.50);
                break;
            case "thursday":
                console.log(price += 8.50);
                break;
            case "friday":
                console.log(price += 8.50);
                break;
            case "saturday":
                console.log(price += 15);
                break;
            case "sunday":
                console.log(price += 15);
                break;
            default:
                console.log("error");
                break;
        }
    }
    if (movie == "casablanca") {
        switch (day) {
            case "monday":
                console.log(price += 8);
                break;
            case "tuesday":
                console.log(price += 8);
                break;
            case "wednesday":
                console.log(price += 8);
                break;
            case "thursday":
                console.log(price += 8);
                break;
            case "friday":
                console.log(price += 8);
                break;
            case "saturday":
                console.log(price += 10);
                break;
            case "sunday":
                console.log(price += 10);
                break;
            default:
                console.log("error");
                break;
        }
    }
    if (movie == "the wizard of oz") {
        switch (day) {
            case "monday":
                console.log(price += 10);
                break;
            case "tuesday":
                console.log(price += 10);
                break;
            case "wednesday":
                console.log(price += 10);
                break;
            case "thursday":
                console.log(price += 10);
                break;
            case "friday":
                console.log(price += 10);
                break;
            case "saturday":
                console.log(price += 15);
                break;
            case "sunday":
                console.log(price += 15);
                break;
            default:
                console.log("error");
                break;
        }
    }
}

movieDay(["the wizard of oz", "monday"]);

 

answered by user matthew44
...