Back to practice

Annotate a numeric function

Difficulty: Easy

TypeScript

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.

  1. Read the examples and the required return value.
  2. Edit the supplied function in the editor, keeping its name and parameters. Return your answer, then select Run code.
  3. Use the test feedback to try again. Hints are available without replacing your code.

Sign in to save items for review and keep private notes.

Implement double(value: number): number to return twice the supplied finite number. Return a numeric value rather than printing it or converting it to text.

What this practices

A TypeScript function signature documents the input and output types. The implementation must also satisfy the arithmetic contract: a function can return the right type while calculating the wrong value.

Work through an example

Input -2.5 produces -5. Doubling preserves the sign of a nonzero input; it does not mean taking an absolute value. Input 0 returns 0, and input 3 returns 6.

Edge cases to check

  • Zero is valid input.
  • Negative and fractional values are allowed.
  • The output must be a number, not a string such as "6".

TypeScript details

TypeScript's number uses JavaScript numeric behavior, including floating-point arithmetic. A number annotation does not mean integer, and it does not validate external JSON at runtime. This exercise supplies valid numeric inputs; do not add parsing, rounding, or string formatting. The annotation disappears when TypeScript is compiled, while the arithmetic still executes.

Check your reasoning

Explain why a function that always returns zero could pass type checking but fail this exercise. Use both a positive and a negative input to test behavior.

Examples

00
36
-2.5-5

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 boundaries
TypeScript

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.

Reset your code?

This replaces your current code with the starter and stops any running tests.