TypeScript bootcamp · Lab 22

Power by recursion

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 pow(base, exp) recursively (exp ≥ 0). Read base and exp; log the result.

Input. Two integers base exp.

Output. base^exp.

Examples

Example 1
Input
2 10
Output
1024
Example 2
Input
5 0
Output
1
Hint
  1. Base case exp === 0 return 1.
Show correct code

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

function pow(base, exp) {
  if (exp === 0) return 1;
  return base * pow(base, exp - 1);
}
const [base, exp] = readLine().trim().split(/\s+/).map(Number);
console.log(pow(base, exp));
main.tstsc · readLine() · Ctrl + Enter
ResultIdle
Run to see output. Check grades the tests.