JavaScript times Function Challenge
This challenge asks you to implement a JavaScript function that repeatedly executes a given callback function a specified number of times. This is a common pattern for performing repetitive tasks, generating sequences, or simulating events, and understanding how to create such a utility is fundamental in JavaScript programming.
Problem Description
Your task is to create a JavaScript function named times that accepts two arguments:
n: A non-negative integer representing the number of times the callback function should be executed.callback: A function that will be executedntimes. This callback function will optionally receive the current iteration index (starting from 0) as its only argument.
The times function should execute the callback function exactly n times. The return value of times is not strictly defined but should ideally be something useful, such as an array containing the results of each callback invocation, or simply undefined if no return value is needed. For this challenge, let's aim for returning an array of the results.
Key Requirements:
- The function must be named
times. - It must accept an integer
nand a functioncallback. - The
callbackfunction should be executedntimes. - The
callbackfunction should receive the current iteration index (0 ton-1) as an argument. - The
timesfunction should return an array containing the return values of eachcallbackinvocation, in order.
Edge Cases to Consider:
- When
nis 0, thecallbackshould not be executed, and an empty array should be returned. - Ensure the loop iterates correctly from 0 up to (but not including)
n.
Examples
Example 1:
// Input:
const n1 = 3;
const callback1 = (i) => `Iteration ${i}`;
const result1 = times(n1, callback1);
// Output:
// ["Iteration 0", "Iteration 1", "Iteration 2"]
// Explanation:
// The callback function is executed 3 times.
// For the first execution (index 0), it returns "Iteration 0".
// For the second execution (index 1), it returns "Iteration 1".
// For the third execution (index 2), it returns "Iteration 2".
// These return values are collected into an array.
Example 2:
// Input:
const n2 = 5;
const callback2 = (i) => i * 2;
const result2 = times(n2, callback2);
// Output:
// [0, 2, 4, 6, 8]
// Explanation:
// The callback function is executed 5 times.
// For index 0, returns 0 * 2 = 0.
// For index 1, returns 1 * 2 = 2.
// For index 2, returns 2 * 2 = 4.
// For index 3, returns 3 * 2 = 6.
// For index 4, returns 4 * 2 = 8.
// These results are collected into an array.
Example 3: (Edge Case)
// Input:
const n3 = 0;
const callback3 = (i) => "This should not be called";
const result3 = times(n3, callback3);
// Output:
// []
// Explanation:
// When n is 0, the callback is not executed at all, and an empty array is returned.
Constraints
nwill be a non-negative integer (0 <= n <= 1000).callbackwill always be a valid JavaScript function.- The total time complexity of your
timesfunction should be O(n) for optimal performance.
Notes
- Consider using a standard
forloop for iteration. - Think about how you will collect the return values from each
callbackexecution. - The problem emphasizes clarity and correctness in handling the iteration count and passing the index to the callback.