作业2

作业2

//重构五种api

var a = new Array(1, 2, 3, 4, 5, 6);
console.log("原数组", a);

//重构shift

Array.prototype.myShift = function() {
    for (var i = 0; i < this.length - 1; i++) {
        this[i] = this[i + 1];
    }
    this.length--;
    return this.length;
}

console.log('shift', a.myShift(), a);

//重构unshift

Array.prototype.myUnshift = function(a) {
    for (var i = this.length; i > 0; i--) {
        this[i] = this[i - 1];
    }
    this[0] = a;
    return this.length;
}

console.log('unshift', a.myUnshift(222), a);

//重构every

Array.prototype.myEvery = function(f) {
    for (var i = 0; i < this.length; i++) {
        if (!f(this[i], i, this)) {
            return false;
        }
    }
    return true;
}

console.log('every', a.myEvery(function(item){
    return item > 10;
}), a);

// 重构forEach

Array.prototype.myForEach = function(f) {
    for (var i = 0; i < this.length; i++) {
        f(this[i], i, this);
    }
}

console.log('forEach', a.myForEach(function(item, index, a){
    a[index] += item;
}), a);

// 重构some

Array.prototype.mySome = function(f) {
    for (var i = 0; i < this.length; i++) {
        if (f(this[i], i, this)) {
            return true;
        }
    }
    return false;
}

console.log('some', a.mySome(function(item){
    return item == 444;
}), a);

作业2

// 数组去重

var a = new Array(1, 2, 1, 2, 53, 4, 7);

//1. 使用原生for循环

var a1 = new Array();
var index1 = 0;
for (var i = 0; i < a.length; i++) {
    var flg = true;
    for (var j = 0; j < a1.length; j++) {
        if (a[i] == a1[j]) {
            flg = false;
        }
    }
    if (flg) {
        a1[index1++] = a[i];
    }
}

console.log(a1);

// 2.使用增强for循环

var a2 = new Array();
var index2 = 0;

for (var i in a) {
    var flg = true;
    for (var j in a2) {
        if (a[i] == a2[j]) {
            flg = false;
        }
    }
    if (flg) {
        a2[index2++] = a[i];
    }
}

console.log(a2);

// 3.使用forEach

var a3 = new Array();
var index3 = 0;
a.forEach(function(item) {
    var flg = true;
    for (var i in a3) {
        if (item == a3[i]) {
            flg = false;
        }
    }
    if (flg) {
        a3[index3++] = item;
    }
})

console.log(a3);

// 4.使用every

var a4 = new Array();
var index4 = 0;

a.every(function(item) {
    var flg = true;
    for (var i in a4) {
        if (item == a4[i]) {
            flg = false;
        }
    }
    if (flg) {
        a4[index4++] = item;
    }
    return true;
})

console.log(a4);

// 5.使用some

var a5 = new Array();
var index5 = 0;

a.some(function(item) {
    var flg = true;
    for (var i in a5) {
        if (item == a5[i]) {
            flg = false;
        }
    }
    if (flg) {
        a5[index5++] = item;
    }
    return false;
})

console.log(a5);

作业3

// 数组扁平化

var a = [1,2,3,[4,5,[6,7,8,[9,10,11]]]];

var newArray = new Array();

var index = 0;

function solve(a) {
    for (i in a) {
        if (typeof a[i] == 'number') {
            newArray[index++] = a[i];
        } else {
            solve(a[i]);
        }
    }
}

solve(a);

console.log(newArray);
点赞

发表回复

电子邮件地址不会被公开。必填项已用 * 标注