FrontendArk

Master Frontend Interviews

  • Practice
  • Playground
  • Resource
Medium

Input Control

20mins

Implement a function createInputControl(initialValue) that manages an input's state and tracks extra properties:

  • touched: true when the input has been "focused then blurred" (you can simulate blur by explicitly calling handleBlur()).
  • dirty: true after the input's value has been changed at least once.
  • different: true if the current value differs from the initial value.

The function must return:

  • value: the current input value
  • touched, dirty, different: booleans tracking input state
  • handleChange(newValue): updates the value
  • handleBlur(): simulates blur
  • reset(): resets everything back to the initial value and clears all states

Example

const nameInput = createInputControl("Oliver"); console.log(nameInput.value); // "Oliver" console.log(nameInput.touched); // false console.log(nameInput.dirty); // false console.log(nameInput.different); // false nameInput.handleChange("Oli"); console.log(nameInput.value); // "Oli" console.log(nameInput.dirty); // true console.log(nameInput.different); // true nameInput.handleBlur(); console.log(nameInput.touched); // true nameInput.reset(); console.log(nameInput.value); // "Oliver" console.log(nameInput.touched); // false console.log(nameInput.dirty); // false console.log(nameInput.different); // false

Code Editor

JavaScript Function

00:00
Loading...

Test Cases

No test results yet

Click "Run" to execute tests