Array.prototype.lengthintermediateconst arr = [1, 2, 3, 4, 5]
arr.length = 3
arr // β [1, 2, 3] (elements truncated)Assign a smaller value to length to truncate an array in place.
You can shorten an array by assigning a smaller value to its length property. Elements beyond the new length are removed and the array is mutated in place.
Challenge: Implement truncate(arr, n) that sets arr.length = n and returns the (now shorter) array.
truncate([1, 2, 3, 4, 5], 3) // β [1, 2, 3]
truncate([1, 2, 3], 0) // β []