Solution: lb_imdb
JavaScript·9 min read·Apr 8, 2025
Here is the commented solution for this project.
In your projects directory, create a new file named lb_imdb.js.
$ touch lb_imdb.js
Open the file with in your text editor and declare two variables named genre and decade.
const genre = 'War';
const decade = 1990;
Use the switch statement to list all the possible values of the genre variable based on the provided movie list.
const genre = 'War';
const decade = 1990;
switch (genre) {
case 'Action':
//
break;
case 'War':
//
break;
case 'Comedy':
//
break;
}
For each pattern of the switch statement, use the if...else if statements combined with the strict equality operator === to output a movie title based on the value of the decade variable.
const genre = 'War';
const decade = 1990;
switch (genre) {
case 'Action':
if (decade === 1990) {
console.log('Timecop (1994) – 99 minutes');
} else if (decade === 2000) {
console.log('Gladiator (2000) – 155 minutes');
}
break;
case 'War':
if (decade === 1990) {
console.log('Saving Private Ryan (1998) – 169 minutes');
} else if (decade === 2000) {
console.log('Black Hawk Down (2001) – 144 minutes');
}
break;
case 'Comedy':
if (decade === 2000) {
console.log('The Hangover (2009) – 100 minutes');
}
break;
}
Use the else statement to output an error message if the value of the decade variable doesn't match any of the tested values.
const genre = 'War';
const decade = 1990;
switch (genre) {
case 'Action':
if (decade === 1990) {
console.log('Timecop (1994) – 99 minutes');
} else if (decade === 2000) {
console.log('Gladiator (2000) – 155 minutes');
} else {
console.log('Error: Movie not found');
}
break;
case 'War':
if (decade === 1990) {
console.log('Saving Private Ryan (1998) – 169 minutes');
} else if (decade === 2000) {
console.log('Black Hawk Down (2001) – 144 minutes');
} else {
console.log('Error: Movie not found');
}
break;
case 'Comedy':
if (decade === 2000) {
console.log('The Hangover (2009) – 100 minutes');
} else {
console.log('Error: Movie not found');
}
break;
}
Finally, add a default case that outputs an error message if the value of the genre variable is not in the list of patterns.
const genre = 'War';
const decade = 1990;
switch (genre) {
case 'Action':
// ...
break;
case 'War':
// ...
break;
case 'Comedy':
// ...
break;
default:
console.log('Error: Movie not found');
break;
}
💡 Here, the
// ...comments are only used to "hide" the lines of code that were already written in the previous code snippets.