my codeacademy course

来源:互联网 发布:她理财是否可靠 知乎 编辑:程序博客网 时间:2024/06/05 11:40
/*jshint multistr:true */


var text = "chendan good huangqian chenyihan huangyucai \
huangqian chenshuang \
chuanzhiping wanglingling chendan chenyihan google";


var myName = "chendan";
var tmp = [];
var hits = [];


Array.prototype.compare = function (array) {
    // if the other array is a falsy value, return
    if (!array)
        return false;


    // compare lengths - can save a lot of time
    if (this.length != array.length)
        return false;


    for (var i = 0, l=this.length; i < l; i++) {
        // Check if we have nested arrays
        if (this[i] instanceof Array && array[i] instanceof Array) {
            // recurse into the nested arrays
            if (!this[i].compare(array[i]))
                return false;
        }
        else if (this[i] != array[i]) {
            // Warning - two different object instances will never be equal: {x:20} != {x:20}
            return false;
        }
    }
    return true;
}


for(var i=0;i< text.length;i++){
    tmp.push(text[i]);
    if(tmp.compare(['c','h','e','n','d','a','n']) === true){
        console.log("you get it!");
        for(j=0;j<7;j++){
            hits.push(tmp.shift());
        }
        tmp=[];
    }else if(tmp.length === 7){
        tmp.shift();
    }
}


if(hits === []){
    console.log("Your name wasn't found!");
}else{
    console.log(hits);
}
0 0
原创粉丝点击