Implement a utility function useArray that manages an array with convenient helper methods.
Instead of manually cloning and mutating arrays, useArray provides a clean interface for common operations like add, remove, update, filter, and clear.
Your task: Write a function useArray(defaultValue) that:
defaultValue.array: the current array.set(newArray): replace the array with newArray.push(item): add an item to the end.remove(index): remove the item at the given index.filter(predicate): keep items matching the predicate.update(index, newItem): replace item at index.clear(): empty the array.Example
const fruits = useArray(['apple', 'banana']); fruits.push('orange'); console.log(fruits.array); // ["apple", "banana", "orange"] fruits.update(1, 'grape'); console.log(fruits.array); // ["apple", "grape", "orange"] fruits.remove(0); console.log(fruits.array); // ["grape", "orange"] fruits.filter(fruit => fruit.includes('a')); console.log(fruits.array); // ["grape", "orange"] fruits.clear(); console.log(fruits.array); // []
JavaScript Function
No test results yet
Click "Run" to execute tests