The task was to add a last() method to Array.prototype.
My immediate intuition was to use this, because the method is reused and invoked at the call site. Runtime-bound, call-site-dependent behavior fits perfectly here.
(2/5)
Here was my solution:
Array.prototype.last = function () {
if (this.length === 0) return -1;
return this[this.length - 1];
};
(4/5)
#JavaScript#DSA#LeetCode
What made this special wasn’t the code, but realizing I reached for this instinctively and correctly.
That’s what good teaching does. Thank you Kyle Simpson.
(5/5)