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 valuetouched, dirty, different: booleans tracking input statehandleChange(newValue): updates the valuehandleBlur(): simulates blurreset(): resets everything back to the initial value and clears all statesExample
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
JavaScript Function
No test results yet
Click "Run" to execute tests