An authenticator app's code changes every 30 seconds because it is computed from counter = floor(time ÷ 30).
What to build
For each Unix time, print the TOTP counter and how long until the code changes.
Requirements
- Use a 30-second step.
- Print
counter C, changes in S s, where S is 30 − (time mod 30).
Input
One Unix time (whole seconds) per line.
Output
One line per time.
Constraints
0 ≤ time ≤ 10¹¹.
Example
Input:
59
60
Output:
counter 1, changes in 1 s
counter 2, changes in 30 s
Why it works
Every time inside the same 30-second window divides to the same whole number, so phone and server get the same counter, and the remainder says how far into the window the clock is.
Examples
Example 1 · Input
59 60
Expected output
counter 1, changes in 1 s counter 2, changes in 30 s
Example 2 · Input
1111111109
Expected output
counter 37037036, changes in 1 s
Hints
Hint 1
Math.floor(t / 30) and t % 30.
Solution
Show one correct solution (try it yourself first)
let line = prompt();
while (line !== null) {
const t = Number(line);
console.log(`counter ${Math.floor(t / 30)}, changes in ${30 - (t % 30)} s`);
line = prompt();
}
▶ Open this solution in the compiler, then press Visualize to watch it run step by step