TypeScript bootcamp · Lab 08

Typed factorial

mediumFunctions12 minLesson: Functions

Read the question, write TypeScript on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Write function factorial(n: number): number. 0! is 1. Read n and log the result.

Examples

Example 1
Input
5
Output
120
Example 2
Input
0
Output
1
Hint
  1. Annotate the parameter and the return type.
Show correct code

Peek only after you have tried. You can still Check your own version.

function factorial(n: number): number {
  let result = 1;
  for (let i = 2; i <= n; i++) result *= i;
  return result;
}

const n: number = Number(readLine());
console.log(factorial(n));
main.tstsc · readLine() · Ctrl + Enter
ResultIdle
Run to see output. Check grades the tests.