Keep string output explicit
Difficulty: Easy
Try your first run
No account is needed to try this exercise. Write your answer, run the tests, and use the feedback to improve it.
- Read the examples and the required return value.
- Edit the supplied function in the editor, keeping its name and parameters. Return your answer, then select Run code.
- Use the test feedback to try again. Hints are available without replacing your code.
Implement greeting(name: string): string. Trim surrounding JavaScript whitespace and return "Hello, NAME!" using the trimmed name. If the trimmed name is empty, return "Hello, friend!". Preserve case, accents, and whitespace inside the name.
What this practices
Separate normalization from a fallback decision. An input can contain characters but become empty after trimming. The decision therefore depends on the cleaned string rather than the original length.
Work through an example
" Ada Lovelace " becomes "Ada Lovelace", producing "Hello, Ada Lovelace!". The internal space remains. A tab-only input becomes empty and produces "Hello, friend!".
Edge cases to check
- An empty input and whitespace-only input use the fallback.
- Tabs and line breaks at the ends are whitespace too.
"Éva"retains its accent and case.- Internal repeated spaces are preserved, not collapsed.
TypeScript details
trim() returns a new string; it does not modify the input. TypeScript can infer the local cleaned value as a string, while the function signature explicitly declares the boundary. An empty string is falsey in JavaScript, but the literal string "0" is not. Do not treat a nonempty numeric-looking name as missing.
Return exactly the greeting, including its comma, space, and exclamation mark.
Examples
' Ada ''Hello, Ada!''\t''Hello, friend!''Éva''Hello, Éva!'Constraints
Use strict TypeScript and a standalone function. No imports, packages, DOM, network, or file access. Inputs are JSON-compatible with at most 1,000 elements and 100,000 characters. Do not mutate inputs. Return the specified result rather than printing. Medium tasks combine narrowing and typed transformations.
Review lesson: Inference and explicit boundariesA little nudge
Reveal one hint at a time. Give yourself a moment to try it.
- trim() returns a new string.
- The contract explicitly allows a fallback for empty text.
Worked solution
Try your own approach first, then compare the reasoning.
function greeting(name: string): string {
const clean = name.trim();
return `Hello, ${clean || "friend"}!`;
}
Trim once, then choose the cleaned string or friend when it is empty. The local clean variable is inferred as string. Interpolation constructs the final string while preserving accents and internal spacing. Checking the untrimmed name would mishandle inputs containing only whitespace.
Complexity
O(n) time and O(n) output space in the worst case for an input of n UTF-16 code units.
Ready when you are
Write your solution, then run it against the practice tests.
Press Esc, then Tab to leave the editor. Code runs in an isolated browser workspace. Tests support learning, not verified grading.