Back to js-datatype-intermediate
03-bollywood-title-fixer.js
JavaScript
1/**
2 * 🎬 Bollywood Movie Title Fixer
3 *
4 * Pappu ne ek movie database banaya hai lekin usne saare titles galat type
5 * kar diye - kuch ALL CAPS mein, kuch all lowercase mein, kuch mein extra
6 * spaces hain. Tu fix kar de titles ko proper Title Case mein!
7 *
8 * Rules:
9 * - Extra spaces hatao: leading, trailing, aur beech ke multiple spaces ko
10 * single space banao
11 * - Har word ka pehla letter uppercase, baaki lowercase (Title Case)
12 * - EXCEPTION: Chhote words jo Title Case mein lowercase rehte hain:
13 * "ka", "ki", "ke", "se", "aur", "ya", "the", "of", "in", "a", "an"
14 * LEKIN agar word title ka PEHLA word hai toh capitalize karo
15 * - Hint: Use trim(), split(), map(), join(), charAt(), toUpperCase(),
16 * toLowerCase(), slice()
17 *
18 * Validation:
19 * - Agar input string nahi hai, return ""
20 * - Agar string trim karne ke baad empty hai, return ""
21 *
22 * @param {string} title - Messy Bollywood movie title
23 * @returns {string} Cleaned up Title Case title
24 *
25 * @example
26 * fixBollywoodTitle(" DILWALE DULHANIA LE JAYENGE ")
27 * // => "Dilwale Dulhania Le Jayenge"
28 *
29 * fixBollywoodTitle("dil ka kya kare")
30 * // => "Dil ka Kya Kare"
31 */
32export function fixBollywoodTitle(title) {
33 if(typeof title !== "string" || title.trim().length === 0){
34 return ""
35 }
36
37 const chhoteWords = ["ka", "ki", "ke", "se", "aur", "ya", "the", "of", "in", "a", "an"]
38 const arrayOfTitle = title.split(" ").map((t)=> t.trim().toLowerCase())
39
40 const UppercaseArray = arrayOfTitle.map((t,id) => {
41 return id === 0? t.charAt(0).toUpperCase()+t.slice(1) : chhoteWords.includes(t)?t: t.charAt(0).toUpperCase()+t.slice(1)
42 })
43 const RemoveEmptySpace = UppercaseArray.filter(t => t.length !== 0)
44 const finalTitle = RemoveEmptySpace.join(" ")
45
46 return finalTitle.trim()
47
48}
49