Currently viewing "Advanced HOIFs, Callbacks, and Closures Questions", use dev tools for the js

Create a function that takes another function as an argument and calls it after 3 seconds (HOF + Callback).

Explanation: A higher-order function (HOF) is a function that takes another function as an argument. In this case, we use setTimeout() to delay the execution of the callback function by 3 seconds.


                function delayedExecution(callback) {
                    setTimeout(callback, 3000);
                }
                
                // Example usage:
                delayedExecution(() => console.log("Executed after 3 seconds"));
            

Implement a function that returns a function with a preset greeting (Closure).

Explanation: A closure allows a function to remember variables from its outer scope. Here, we return a function that keeps a predefined greeting message.


                function greetingGenerator(greeting) {
                    return function(name) {
                        console.log(\`\${greeting}, \${name}!\`);
                    };
                }
                
                // Example usage:
                let sayHello = greetingGenerator("Hello");
                sayHello("Alice"); // "Hello, Alice!"
                sayHello("Bob");   // "Hello, Bob!"
                
                let sayHi = greetingGenerator("Hi");
                sayHi("Charlie");  // "Hi, Charlie!"
            

Implement a function that takes a callback and only executes it once (HOF + Closure).

Explanation: This function ensures that a callback function is executed only once, no matter how many times it is called. We use a closure to track whether the function has been called before.


                function once(callback) {
                    let executed = false;
                    return function(...args) {
                        if (!executed) {
                            executed = true;
                            return callback(...args);
                        } else {
                            console.log("Function can only be executed once");
                        }
                    };
                }
                
                // Example usage:
                let initialize = once(() => console.log("Initialized!"));
                initialize(); // "Initialized!"
                initialize(); // "Function can only be executed once"
            

Implement a function that throttles another function (HOF + Closures).

Explanation: Throttling ensures that a function is executed at most once in a given time interval, preventing excessive calls. This is useful for performance optimization, such as limiting API requests or controlling event listeners.


                function throttle(func, delay) {
                    let lastCall = 0;
                    return function(...args) {
                        let now = Date.now();
                        if (now - lastCall >= delay) {
                            lastCall = now;
                            func(...args);
                        }
                    };
                }
                
                // Example usage:
                function logMessage() {
                    console.log("Throttled function executed at", new Date().toLocaleTimeString());
                }
                
                let throttledLog = throttle(logMessage, 2000);
                
                // Call multiple times, but only executes every 2 seconds
                setInterval(throttledLog, 500);