TypeScript bootcamp · Lab 14

GCD

mediumWhile12 minLesson: While

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

QuestionHint and solution stay closed until you open them

Read two positive integers and print their greatest common divisor (Euclid).

Input. Two integers.

Output. One integer.

Examples

Example 1
Input
12 18
Output
6
Example 2
Input
7 13
Output
1
Hint
  1. while (b) { [a,b] = [b, a % b]; }
Show correct code

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

let [a, b] = readLine().trim().split(/\s+/).map(Number);
while (b) { const t = a % b; a = b; b = t; }
console.log(a);
main.tstsc · readLine() · Ctrl + Enter
ResultIdle
Run to see output. Check grades the tests.