Read the question, write TypeScript on the right, then Run or Check.
QuestionHint and solution stay closed until you open them
Collatz: even → n/2, odd → 3n+1. Read n; print steps to reach 1 (0 if already 1).
Input. One integer ≥ 1.
Output. Step count.
Examples
Input
6
Output
8
Input
1
Output
0
Hint
- Use Math.floor for even branch.
Show correct code
Peek only after you have tried. You can still Check your own version.
let n: number = Number(readLine());
let steps = 0;
while (n !== 1) {
n = n % 2 === 0 ? Math.floor(n / 2) : 3 * n + 1;
steps++;
}
console.log(steps);
main.tstsc · readLine() · Ctrl + Enter
ResultIdle
Run to see output. Check grades the tests.