Topic Description

Please implement an array method that allows any array to call the array.last() method. This method should return the last element of the array. If the array is empty, it should return -1. You can assume the array is the result of JSON.parse and is a JSON array.

// Example 1:
Input: nums = [null, {}, 3]
Output: 3
Explanation: Calling nums.last() returns the last element: 3.

// Example 2:
Input: nums = []
Output: -1
Explanation: Since the array has no elements, it should return -1.

Answer

Array.prototype.last = function(){
	if(this.length == 0){
		return -1
	}
	return this[this.length-1]
}