var nums = new Array(2, 5, 7, 11);
// 作业1给定一个数组nums=[2,5,7,11],要求任意两个数组元素和等于指定的值target=9;并返回他们下标,return[0,2]
function ans() {
var target = 9;
for (var i = 0; i < nums.length; i++) {
for (var j = i; j < nums.length; j++) {
if (nums[i] + nums[j] == target) {
console.log(new Array(i, j))
}
}
}
}
ans();
// 作业2重构pop方法,要求实现和pop一样的效果
Array.prototype.myPop = function() {
var ans = this[this.length - 1];
this.splice(this.length - 1);
return ans;
}
console.log(nums.myPop(), "myPop结果", nums);
Array.prototype.myPush = function(a) {
this[this.length] = a;
return this.length;
}
console.log(nums.myPush(5), "myPush结果", nums);