美文网首页
遇到的一些面试问题,持续更新

遇到的一些面试问题,持续更新

作者: zhangmz | 来源:发表于2017-08-09 18:12 被阅读7次

1、用JS函数写出,输出两个字符串最长的公共部分

function find(str1, str2) {
            if (str1.length > str2.length) {
                shorter = str2;
                longer = str1
            } else {
                shorter = str1;
                longer = str2;
            }
            for (var subLength = shorter.length; subLength > 0; subLength--) {
                for (var i = 0; i + subLength <= shorter.length; i++) {
                    var subString = shorter.substring(i, i + subLength);
                    // debugger;  
                    if (longer.indexOf(subString) >= 0) {
                        targetString = subString;
                        return targetString;
                    }
                }
            }
        }
        find("instritesting", "string");
        console.log(targetString);

2、输入值

function Parent() {
            this.a = 1;
            this.b = [1, 2, this.a];
            this.c = {demo: 5};
            this.show = function () {
                console.log(this.a, this.b, this.c.demo);
            }
        }
        function Child() {
            this.a = 2;
            this.change = function () {
                debugger;
                this.b.push(this.a);
                this.a = this.b.length;
                this.c.demo = this.a++;
            }
        }
        Child.prototype = new Parent();
        var parent = new Parent();
        var child1 = new Child();
        var child2 = new Child();
        child1.a = 11;
        child2.a = 12;
        parent.show();
        child1.show();
        child2.show();
        child1.change();
        child2.change();
        parent.show();
        child1.show();
        child2.show();

3、parseURL

function parseURL(url) {
    var a =  document.createElement('a');
    a.href = url;
    return {
        source: url,
        protocol: a.protocol.replace(':',''),
        host: a.hostname,
        port: a.port,
        query: a.search,
        params: (function(){
            var ret = {},
                seg = a.search.replace(/^?/,'').split('&'),
                len = seg.length, i = 0, s;
            for (;i<len;i++) {
                if (!seg[i]) { continue; }
                s = seg[i].split('=');
                ret[s[0]] = s[1];
            }
            return ret;
        })(),
        file: (a.pathname.match(//([^/?#]+)$/i) || [,''])[1],
        hash: a.hash.replace('#',''),
        path: a.pathname.replace(/^([^/])/,'/$1'),
        relative: (a.href.match(/tps?://[^/]+(.+)/) || [,''])[1],
        segments: a.pathname.replace(/^//,'').split('/')
    };
}

var myURL = parseURL('http://abc.com:8080/dir/index.html?id=255&m=hello#top');
 
myURL.file;     // = 'index.html'
myURL.hash;     // = 'top'
myURL.host;     // = 'abc.com'
myURL.query;    // = '?id=255&m=hello'
myURL.params;   // = Object = { id: 255, m: hello }
myURL.path;     // = '/dir/index.html'
myURL.segments; // = Array = ['dir', 'index.html']
myURL.port;     // = '8080'
myURL.protocol; // = 'http'
myURL.source;   // = 'http://abc.com:8080/dir/index.html?id=255&m=hello#top'

4、实现js变量的深度克隆

function clone(obj) {
            var str, newobj = obj.constructor === Array ? [] : {};
            if (typeof obj !== "object") {
                newobj = obj;
            } else if (window.JSON) {
                str = JSON.stringify(obj);
                newobj = JSON.parse(str);
            } else {
                for (var key in obj) {
                    newobj[key] = typeof obj[key] === "object?" ? deepClone(obj[key]) : obj[key];
                }
            }
            return newobj;
  }

5、不用循环(包括es5的forEach、map等方法),创建一个长度为100的数组,并且每个元素的值等于它的下标

var arr = Object.keys(Array.apply(null, {length:100})).map(function(item){
        return +item; //+号是一个转换格式的小技巧,你也可以使用parseInt
    });
console.log(arr);
var arr = Array.from({length:100}, (v,k) => k);
console.log(arr);
var arr = new Array(100).keys();
console.log(Array.from(arr));
var arr = Object.keys(Array.from({length:100}));
console.log(arr);
var arr = [...Array(100).keys()];
console.log(arr);
或者可以改写成:
var arr = [...Array.from({length:100}).keys()];
console.log(arr);
var arr = Object.keys(String(Array(101)));
console.log(arr);
var arr = [];
    var i = 0;
    var timer = setInterval(function(){
        arr[i] = i++;
        if(i>=100){
            clearInterval(timer);
            console.log(arr);
        }
    },1);

6、请写出一个打乱js数组[1,2,3,4,5]内元素排序的方法

var arr = [1, 2, 3, 4, 5];
arr.sort(function () {
     return 0.5 - Math.random()
});
console.log(arr);
即
function randomsort(a, b) {
       return Math.random()>.5 ? -1 : 1; //用Math.random()函数生成0~1之间的随机数与0.5比较,返回-1或1
 }
var arr = [1, 2, 3, 4, 5];
arr.sort(randomsort);

相关文章

网友评论

      本文标题:遇到的一些面试问题,持续更新

      本文链接:https://www.haomeiwen.com/subject/qreqrxtx.html