今日頭條基礎算法題

面試題彙總:
今日頭條:
已知如下類Animal,要求設計一個Cat類繼承自Animal,並實現如下功能:

Animal:function Animal(){
    this.name = "Animal";
    this.showName = function(){
        console.log(this.name);
    }
}
 
Cat:function Cat(){
    this.name = "Cat";
    this.showName1 = function(){
        console.log(this.name); 
    }
    this.showName2 = function(){
        console.log(this.name); 
    } 
    this.showName3 = function(){
        console.log(this.__super.name + "=>" + this.name); 
    }
}

代碼運行:
// 請完善Cat部分相關代碼,得到如下結果:

var cat = new Cat();
console.log(cat instanceof Animal ); // 得到:true
cat.showName1();     // 得到:"Cat" (需要讀到Cat中的name屬性) 
cat.showName2();    //  得到:”Animal" (需要讀到Animal中的name屬性) 
cat.showName3();    //得到:”Animal" => "Cat" (需要同時讀到Cat中的name和Animal中的name)

解析:

function Animal() {
    this.name = "Animal";
    this.showName = function() {
        console.log(this.name);
    };
}
function Cat() {
    this.name = "Cat";
    this._super = Cat.prototype;
 
    this.showName1 = function() {
        console.log(this.name);
    };
    this.showName2 = function() {
        console.log(this.name);
    };
    this.showName3 = function() {
        console.log(this._super.name + "=>" + this.name);
    };
}
Cat.prototype = new Animal();
var cat = new Cat();
console.log(cat instanceof Animal);   //true
cat.showName1();     //"Cat"
cat.showName2.call(Cat.prototype);   //"Animal"
cat.showName3();    //"Animal" => "Cat"

第二題:
已知道如下數組:

var arr = [[1,2,2],[3, 4, 5, 5],[6, 7, 8, 9,[11,12,[12,13,[14]]]],10];

編寫一個程序將數組扁平化去併除其中重複部分數據,最終得到一個升序且不重複的數組:

var res= [1,2,3,4,5,6,7,8,9,10,11,12,13,14]

解析:

/**
 * 解析數組,然後去重,排序
 * @type Array
 */
<strong>//解析數組</strong>
var arr = [[1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14]]]], 10];
var newArray = [];
function getArray(array) {
    array.forEach(function(e) {
        if (typeof e === "object") {
            getArray(e);
        } else {
            newArray.push(e);
        }
    });
}
getArray(arr);
<strong>//去重</strong>
Array.prototype.distinct = function() {
    return this.reduce(function(newArray1, newValue) {
        if (newArray1.indexOf(newValue) === -1)
            newArray1.push(newValue);
        return newArray1;
    }, []);
};
newArray = newArray.distinct();
<strong>//排序</strong>
newArray.sort(function(a, b) {
    return a - b;
});
console.log(newArray);

第三題:
如下題目存在哪些問題(改錯)?

var obj = {
    name: " jsCoder",
    skill: ["css3","html5", "es6", "react", "angular"],
    say: function () {      
        for(var i = 0, len = this.skill.length; i< len; i++){
            setTimeout(function(){
                console.log("No." + i + this.name);
                console.log(this.skill[i]);
                console.log('--------------------------');
            },100);
        }
    }
}
obj.say();

如何得到結果,能想到幾種方法:

<span style="font-size:18px;">No.1 jsCoder
css3
--------------------------
No.2 jsCoder
html5
--------------------------
No.3 jsCoder
es6
--------------------------
No.4 jsCoder
react
--------------------------
No.5 jsCoder
angular
-------------------------- </span>

解析:

<span style="font-size:18px;">/**
 * 考察this指向、閉包
 *  
 */
var obj = {
    name: " jsCoder",
    skill: ["css3", "html5", "es6", "react", "angular"],
    say: function() {
        for (var i = 0, len = this.skill.length; i < len; i++) {
            (function() {
                var temp = i;
                setTimeout(function() {
                    console.log("No." + temp + obj.name);
                    console.log(obj.skill[temp]);
                    console.log('--------------------------');
                }, 100);
            })();
        }
    }
};
obj.say();</span>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章