Sortable Movies in JavaScript
Building Dynamic Movie Database Interfaces with JavaScript
This is part one of a multi-part series covering how to build a complete sortable movie database display in JavaScript, progressing from data formatting to rendering and sorting functionality.
Key Features We'll Build
Time Format Conversion
Transform raw minutes into user-friendly hour and minute display format. Convert 134 minutes to 2h14m format for better readability.
Dynamic Sorting
Implement multi-criteria sorting capabilities for movie collections. Users can sort by name, duration, or release year.
Database Rendering
Display movie data in an organized, interactive interface. Present information in a clean, accessible format for users.
We don't want it to say 134 minutes. We want it to say 2h14m like IMDb does.
Time Conversion Implementation Process
Set up the loop
Initialize a for loop to iterate through the movies array using standard counter variables and length property
Extract minutes value
Retrieve the mins property from each movie object to get the raw minute duration for conversion
Calculate hours
Divide minutes by 60 and use Math.floor to round down to get the whole number of hours
Find remainder minutes
Use the modulus operator to get the remaining minutes after extracting full hours
Create formatted string
Add new ourMin property using template literals to format as 'Xh Ym' display format
Be careful with template literal syntax - the dollar sign must be outside the parentheses, not inside. Incorrect: `(${hours})` Correct: `${hours}`
Before vs After Data Format
| Feature | Original Format | Improved Format |
|---|---|---|
| Black Panther Duration | 134 minutes | 2h14m |
| User Experience | Raw numerical data | Intuitive time format |
| Display Consistency | Technical format | Industry standard |
JavaScript Operators Used
Math.floor()
Rounds down to the nearest integer, essential for converting decimal hours to whole numbers. Takes any number and returns the largest integer less than or equal to it.
Modulus Operator (%)
Returns the remainder after division, perfect for extracting leftover minutes. Divides the first number by the second and gives you what's left over.
Template Literals
Allows embedded expressions in strings using backticks and ${} syntax. Provides clean string interpolation for formatted output like time displays.
Key Takeaways