JavaScript Array and Object Fundamentals Quiz
Test the day-one moves for working with arrays and objects: type-checking, copying, deduping, and the gotchas hiding under shallow vs deep clones.
759 views
21
Implement a isArr function that returns true for arrays and false for everything else, including array-like objects and typed arrays.
Examples
Example 1:
Input: isArr([1, 2, 3]), isArr('abc'), isArr({ length: 2 }), isArr(new Uint8Array(2))
Output: true, false, false, false
Explanation: Array.isArray returns true only for genuine Array instances; typed arrays and array-like objects fail the check.function isArr(value) {
// your code here
}Write shallowCopy so that it returns a new top-level copy of an object, but assert with the included logs that nested objects still share references with the original.
Examples
Example 1:
Input: const a = { x: 1, nested: { y: 2 } }; const b = shallowCopy(a); b.nested.y = 99; console.log(a.nested.y)
Output: 99
Explanation: A shallow copy duplicates only the top-level keys, so `nested` is the same reference in both objects.function shallowCopy(obj) {
// your code here
}
const a = { x: 1, nested: { y: 2 } };
const b = shallowCopy(a);
b.nested.y = 99;
console.log(a.nested.y); // ?Implement removeDuplicates(arr) that returns the unique values of arr in original order, without mutating the input.
Examples
Example 1:
Input: removeDuplicates([1, 2, 2, 3, 1, 4])
Output: [1, 2, 3, 4]
Explanation: A Set keeps insertion order while discarding duplicates; spread back into an array.function removeDuplicates(arr) {
// your code here
}Write deepClone(obj) using a JSON-based approach and list one concrete value type it cannot round-trip.
Examples
Example 1:
Input: deepClone({ a: 1, d: new Date(0), f: () => 1 })
Output: { a: 1, d: '1970-01-01T00:00:00.000Z' } // function is dropped, Date becomes a string
Explanation: JSON.stringify can only preserve JSON-representable values, so Date, Map, Set, RegExp, functions, and undefined are all lost or coerced.function deepClone(obj) {
// your code here
}Write removeDuplicates(iterable) that accepts any iterable (array, string, generator) and returns an array of unique items in order.
Examples
Example 1:
Input: removeDuplicates([1, 1, 2, 3]), removeDuplicates('foobar')
Output: [1, 2, 3], ['f', 'o', 'b', 'a', 'r']
Explanation: Iterating with `for...of` works on any iterable, and a `Set` enforces uniqueness while keeping insertion order.function removeDuplicates(iterable) {
// your code here
}