Simple GreetingProblem StatementWrite a function `greet(name)` that returns "Hello, name!". If no name is provided, default to "Guest".Approach 1. Use default parameters for the 'name'. 2. Return a template literal string. Solution`function greet(name = "Guest") { return `Hello, ${name
Arrow Function SumProblem StatementConvert the following into a one-line arrow function: `function add(a, b) { return a + b; }`.Approach 1. Use the `const` keyword. 2. Remove `function` and `return` (implicit return for single-line). Solutionconst add = (a, b) => a + b;
Higher Order: Map ImplementationProblem StatementWrite a function `apply(arr, fn)` that applies the function `fn` to every element of `arr` and returns the new array.Approach 1. Create a result array. 2. Loop through `arr`. 3. Push `fn(element)` to result. Solution`function apply(arr, fn) { const result = []; for (let item of arr) { result.push(fn(item));
Closure CounterProblem StatementWrite a function `createCounter()` that returns another function. The returned function should increment and return a count every time it is called.Approach 1. Define a variable `count` inside `createCounter`. 2. Return a function that modifies and returns that `count`. Solution`function createCounter() { let count = 0; return function() { count++; return count;
Function FactoryProblem StatementWrite a function `makeMultiplier(x)` that returns a function which takes a number `y` and returns `x * y`.Approach 1. This is a classic closure example. 2. The inner function remembers `x`. Solution`function makeMultiplier(x) { return function(y) { return x * y;
Sum Rest ParametersProblem StatementWrite a function `sumAll(...nums)` that uses rest parameters to sum all arguments passed to it.Approach 1. `...nums` collects all arguments into an array. 2. Use `.reduce()` on that array. Solution`function sumAll(...nums) { return nums.reduce((total, n) => total + n, 0);
IIFE ConceptProblem StatementWrite an Immediately Invoked Function Expression (IIFE) that logs "Running..." to the console.Approach` 1. Wrap function in parentheses: `(function(){...Solution`(function() { console.log("Running...");
Recursive FactorialProblem StatementWrite a recursive function `factorial(n)` that handles the base case (0 or 1) and calls itself otherwise.Approach 1. Base case: `if (n <= 1) return 1`. 2. Recursive step: `return n * factorial(n - 1)`. Solution`function factorial(n) { if (n <= 1) return 1; return n * factorial(n - 1);
Delayed CallbackProblem StatementWrite a function `delayedCall(callback)` that executes the `callback` after a 100ms delay using `setTimeout`.Approach 1. Use `setTimeout`. 2. Pass the callback function and the delay time. Solution`function delayedCall(callback) { setTimeout(callback, 100);
Find Max in ArgumentsProblem StatementWrite a function `maxOfMany(...nums)` that returns the largest of all arguments provided.Approach 1. Use `...nums` to get an array. 2. Use `Math.max(...nums)` with the spread operator. Solution`function maxOfMany(...nums) { if (nums.length === 0) return undefined; return Math.max(...nums);
Private Variables (Closure)Problem StatementCreate an object generator `createSecret(val)` that returns an object with two methods: `getVal()` and `setVal(newVal)`. The original `val` should not be accessible directly.Approach 1. Keep `val` in the outer scope (lexical scope). 2. Return methods that closure over `val`. Solution`function createSecret(val) { let secret = val; return { getVal: () => secret, setVal: (newVal) => { secret = newVal;
Validation: Is Function?Problem StatementWrite a function `isValidCallback(fn)` that returns `true` if the input is a function.Approach 1. Use `typeof fn === 'function'`. Solution`function isValidCallback(fn) { return typeof fn === 'function';
Partial ApplicationProblem StatementWrite a function `partial(fn, ...args)` that returns a new function which, when called with more arguments, combines those with the original ones.Approach 1. Capture `fn` and initial `args`. 2. Return a function taking `moreArgs`. 3. Call original `fn` with both combined. Solution`function partial(fn, ...args) { return function(...moreArgs) { return fn(...args, ...moreArgs);
Simple MemoizerProblem StatementWrite a function `memoize(fn)` that returns a function which caches the results of function calls based on its arguments.Approach 1. Create a `cache` object inside the closure. 2. Convert arguments to a string to use as a key. 3. If key in cache, return result. Else, calculate, store, and return. Solution`function memoize(fn) { const cache = {
Call OnceProblem StatementWrite a function `once(fn)` that ensures the original function is only called one time, returning the same result on subsequent calls.Approach 1. Use a flag `called` and a variable `result`. Solution`function once(fn) { let called = false; let result; return function(...args) { if (!called) { called = true; result = fn(...args);
Simple CurryingProblem StatementConvert `add(a, b, c)` into a curried version: `add(a)(b)(c)`.Approach 1. Return a function that returns a function... Solution`function add(a) { return function(b) { return function(c) { return a + b + c;
Function CompositionProblem StatementWrite a function `compose(f, g)` that returns a new function that performs `f(g(x))`.Approach 1. Return a function taking `x`. 2. Apply `g(x)` first, then pass that to `f`. Solution`function compose(f, g) { return function(x) { return f(g(x));
Function PipeProblem StatementWrite a function `pipe(...fns)` that takes multiple functions and returns a function that applies them sequentially to an input.Approach 1. Use `reduce` starting with the initial input `x`. Solution`function pipe(...fns) { return function(x) { return fns.reduce((acc, fn) => fn(acc), x);
Binding ContextProblem StatementWrite a function `callWithPerson(fn, person)` that invokes `fn` with `this` bound to the `person` object.Approach 1. Use `fn.call(person)` or `fn.apply(person)`. Solution`function callWithPerson(fn, person) { return fn.call(person);
Identity FunctionProblem StatementWrite a function `identity(val)` that simply returns the value it received.Approach 1. Return `val`. Frequently used as a default in HOFs. Solution`function identity(val) { return val;