Explorer
Node.js

Write a function `retry(fn, maxRetries)` that retries a function up to maxRetries times with exponential backoff delays (simulated).

Problem Statement

Write a function `retry(fn, maxRetries)` that retries a function up to maxRetries times with exponential backoff delays (simulated).

Complexity

Time Complexity: -

Space Complexity: -

Hints

šŸ’” Hint 1: Exponential backoff doubles the delay after each failure. šŸ’” Hint 2: Catch errors and retry until maxRetries is exhausted. šŸ’” Hint 3: Use a loop with try/catch. Track attempt count. āœ… Solution: ```javascript function retry(fn, maxRetries) { let attempts = 0; while (attempts <= maxRetries) { try { return fn(attempts); } catch (e) { attempts++; if (attempts > maxRetries) throw e; } } } ```

Retry with Exponential Backoff

Medium
Write a function `retry(fn, maxRetries)` that retries a function up to maxRetries times with exponential backoff delays (simulated).
Editor
Loading Editor...
Evaluate code
Output:Click "Run" above to execute and verify your code here.
{"result":"success","callCount":3}