Exercise 11: Check if String Contains Only Letters
Problem Statement
Function `isAlpha(str)` banao jo check kare ki string me sirf letters hain (a-z, A-Z).
Example: isAlpha("Hello") => true, isAlpha("Hello123") => false
Sample Output:
isAlpha("Hello") => true
isAlpha("Hello123") => false
isAlpha("") => falseSolution
const isAlpha = (str) => {
const s = String(str ?? "");
return s.length > 0 && /^[a-zA-Z]+$/.test(s);
};Explanation
Overall Goal:
- String me sirf alphabetic characters hain ya nahi check karna.
Real world:
- Validation: name fields.
- Data cleaning: alphabetic only.