Exercise 4: Case-insensitive Includes

Problem Statement

Create a function `includesCI(text, query)` (case-insensitive).

Sample Output:

includesCI("Hello World", "hello") => true
includesCI("JavaScript", "SCRIPT") => true
includesCI("Python", "java") => false

Solution

const includesCI = (text, query) =>
  String(text ?? '').toLowerCase().includes(String(query ?? '').toLowerCase());

Explanation

Overall Goal:

  • Case-insensitive string search: text me query find karna.
  • "Hello" me "HELLO" bhi match hona chahiye.

Single Line Solution:

  • const includesCI = (text, query) => String(text ?? '').toLowerCase().includes(String(query ?? '').toLowerCase());

Part 1: Normalize text

  • String(text ?? '') → nullish coalescing: null/undefined ho to empty string.
  • .toLowerCase() → sabhi characters lowercase me convert.
  • Example: "Hello World""hello world".

Part 2: Normalize query

  • String(query ?? '').toLowerCase()
  • Same process query ke liye: string convert → lowercase.
  • Example: "HELLO""hello".

Part 3: Case-insensitive search

  • .includes(...) → lowercase text me lowercase query search karta hai.
  • Result: case-insensitive match.
  • Example: "hello world".includes("hello")true.

Real world:

  • Search filters: product search, user search.
  • Autocomplete: suggestions me case-insensitive match.
  • Form validation: email domains check karna.