Hone logo
Hone
Problems

Implementing a Constant Function in JavaScript

In programming, a constant function is a function that always returns the same value, regardless of any input it receives. This is a fundamental concept that can be useful in various scenarios, such as setting default values, creating immutable data sources, or simplifying complex logic. Your task is to implement such a function.

Problem Description

You need to create a JavaScript function named constant. This function should accept a single argument, which will be the value that the returned function will always produce. The constant function itself should then return another function. This inner, returned function, when called, should disregard any arguments passed to it and consistently return the value that was originally provided to constant.

Key Requirements:

  • The constant function must accept one argument.
  • The constant function must return a new function.
  • The returned function must always return the value passed to constant.
  • The returned function should accept any number of arguments, but these arguments must be ignored.

Expected Behavior:

When you call constant with a specific value, and then call the function it returns, you will always get that specific value back, no matter what you pass to the returned function.

Edge Cases:

  • Consider what happens if constant is called without any arguments.
  • Consider what happens if the argument passed to constant is undefined or null.

Examples

Example 1:

Input: constant(10)
Output: A function that always returns 10.

Call the returned function:
const alwaysTen = constant(10);
console.log(alwaysTen());        // Output: 10
console.log(alwaysTen(1, 2, 3)); // Output: 10
console.log(alwaysTen("hello")); // Output: 10

Explanation: The constant function is called with 10. It returns a new function. When this new function (alwaysTen) is called, it ignores any arguments ((), (1, 2, 3), ("hello")) and always returns the original value 10.

Example 2:

Input: constant("hello world")
Output: A function that always returns "hello world".

Call the returned function:
const sayHello = constant("hello world");
console.log(sayHello("any", "arguments")); // Output: "hello world"

Explanation: Similar to the previous example, constant is given the string "hello world". The returned function will always produce this string.

Example 3:

Input: constant(undefined)
Output: A function that always returns undefined.

Call the returned function:
const returnsUndefined = constant(undefined);
console.log(returnsUndefined(5)); // Output: undefined

Explanation: This demonstrates handling undefined as the constant value. The returned function correctly returns undefined.

Constraints

  • The input value passed to constant can be of any JavaScript data type.
  • The returned function can be called with any number of arguments, of any data type.
  • Performance is not a primary concern for this challenge, but the implementation should be efficient.

Notes

Think about how you can capture the initial value provided to constant so that it's accessible to the function you return, even after constant has finished executing. Closures in JavaScript are a powerful tool for this.

Loading editor...
javascript