Exercise 8: Check if String Ends with Suffix

Problem Statement

Function `endsWith(str, suffix)` banao jo check kare ki string suffix se end hoti hai ya nahi (case-insensitive). Example: endsWith("hello.js", ".js") => true

Sample Output:

endsWith("hello.js", ".js") => true
endsWith("file.TXT", ".txt") => true
endsWith("test", ".js") => false

Solution

const endsWith = (str, suffix) => {
  const s = String(str ?? "").toLowerCase();
  const suf = String(suffix ?? "").toLowerCase();
  return s.endsWith(suf);
};

Explanation

Overall Goal:

  • String suffix se end hoti hai ya nahi check karna.

Real world:

  • File validation: extension checks.
  • URL validation: domain checks.