调试中...
调试中...
题目描述
题目描述
题解
题解
提交记录
提交记录
代码
代码
测试用例
测试用例
测试结果
测试结果
简单
相关企业
提示

请你编写一段代码实现一个数组方法,使任何数组都可以调用 array.last() 方法,这个方法将返回数组最后一个元素。如果数组中没有元素,则返回 -1 。

你可以假设数组是 JSON.parse 的输出结果。

 

示例 1 :

输入:nums = [null, {}, 3]
输出:3
解释:调用 nums.last() 后返回最后一个元素: 3。

示例 2 :

输入:nums = []
输出:-1
解释:因为此数组没有元素,所以应该返回 -1。

 

提示:

  • arr 是一个有效的 JSON 数组
  • 0 <= arr.length <= 1000
通过次数
26.7K
提交次数
38.1K
通过率
70.2%

相关企业

提示 1
Inside the Array.prototype.last function body, you have access to the "this" keyword. "this" is equal to the contents of the array in this case.

提示 2
You can access elements in the array via this[0], this[1], etc. You can also access properties and method like this.length, this.forEach, etc.

相似题目

评论 (0)

贡献者
© 2025 领扣网络(上海)有限公司
0 人在线
行 1,列 1
运行和提交代码需要登录
nums =
[null, {}, 3]
Source