Run Callback for a Task Scheduler

You’re working on a task scheduler for a team project. Each task can have additional operations that need to be executed once the main task is completed. These additional operations are given as callback functions.

You need to create a function that runs the main task and then executes the callback function associated with it.

Instructions:

  • Write a function named executeTask that takes two arguments:

    • A task string describing the task.

    • A callback function that will run after the task is completed.

  • The function should:

    • Print a message "Starting task: [task]".

    • Wait for 1 second, simulating task completion (use setTimeout).

    • Then, execute the callback function, which should print "Task complete!" or another message based on the callback passed.

Example 1

Input:

executeTask("Data Backup", function() {
   console.log("Task complete!");
});

Output:

Starting task: Data Backup
(after 1 second) Task complete!

Example 2

Input:

executeTask("File Upload", function() {
   console.log("Task finished with special processing!");
});

Output:

Starting task: File Upload
(after 1 second) Task finished with special processing!

Are you stuck on this problem?

Get detailed, step-by-step solution and improve your JavaScript skills.

  • Detailed Explanation: Each solution comes with a comprehensive explanation, helping you understand the logic and concepts thoroughly.
  • Learn to Write Better Code: Even if you have written the code, discover ways to improve it and adopt best practices.
  • Save Time: Don’t waste hours struggling—get the solution and learn efficiently.
  • Lifetime Access: Get access to lifetime solutions for all problems available
Get the Solution

" I struggled with this problem for hours, but the solution provided here was clear and easy to follow. It helped me understand where I went wrong and improved my coding skills significantly." - Jane D.

If you're not satisfied, I offer a 7-day money-back guarantee.