/**
* 1.给定一个数组nums=[2,5,7,11],要求任意两个数组元素和等于指定的值target=9;并返回他们下标,return[0,2]
*/
var nums = [2,5,7,11];
var target = 9;
var re = [];
for (i = 0; i < nums.length; i++) {
for (j = 1; j < nums.length -1; j++){
if (nums[i] + nums[j] == target){
// console.log(i, j);
re[0] = i;
re[1] = j;
}
}
}
console.log(re);
console.log('--------------------------------------------');
// 2.重构pop方法,要求实现和pop一样的效果
Array.prototype.myPop = function () {
if(this.length >= 1){
var last = this[this.length - 1]; //
this.length--;
return last ;
} else {
return undefined;
}
}
console.log(nums.myPop(), '删除掉的数组元素');
console.log(nums);
console.log('---------------------------------------------------');
//3.重构push方法,要求实现和push一样的效果 不要去查博客 自己写
Array.prototype.myPush = function () {
for (i = 0; i < arguments.length; i++) {
this[this.length] = arguments[i];
}
return this.length;
}
console.log(nums.myPush('Evan', 'Tom'), '返回新数组的长度 ');
console.log(nums);