js面试

2019-05-23  本文已影响0人  风雪之隅_b6f7

1、请写出console结果

setImmediate(function () {

    console.log(7)

});

setTimeout(function () {

    console.log(1)

}, 0);

process.nextTick(function () {

    console.log(6)

    process.nextTick(function () {

        console.log(8)

    })

});

new Promise(function executor(resolve) {

    console.log(2);

    for (var i = 0; i < 10000; i++) {

        i == 9999 && resolve();

    }

    console.log(3);

}).then(function () {

    console.log(4);

});

console.log(5);

//执行队列(同步) 2 3 5  6 8

//任务队列(异步) 4 (1,7)

2、两个有序数组合并成一个有序数组

function sortSTB(arr1,arr2){

     let index1 = 0,index2 = 0;

   let result = [];

   while(index1 !== arr1.length && index2 !== arr2.length){

       if(arr1[index1] <= arr2[index2]){

            result.push(arr1[index1]);

            index1++;

        }else{

             result.push(arr2[index2]);

             index2++;

        }

     }

     if(index1 === arr1.length){

        result = result.concat(arr2.slice(index2));

    }else{

         result = result.concat(arr1.slice(index1));

    }

     return result;

 }

3、12345678 ——> 12,345,678

var num=12345678;

var str=num+"";

function rever(str){

return str=str.split("").reserve().join("");

}

str=rever(str)

var result="";

for(var i=1;i<str.length;i++){

  result=result+str[i-1];

  if(i%3==0&&i!=str.length){

      result+=","

  }

}

result=rever(result)

  console.log(result)

4、尽可能多的方法写一下数组去重

//双层循环

var array = [1, 1, '1', '1'];

function unique(array) {

    var res = [];

    for (var i = 0, arrayLen = array.length; i < arrayLen; i++) {

        for (var j = 0, resLen = res.length; j < resLen; j++ ) {

            if (array[i] === res[j]) {

                break;

            }

        }

        if (j === resLen) {

            res.push(array[i])

        }

    }

    return res;

}

console.log(unique(array)); // [1, "1"]

//利用indexOF

var array = [1, 1, '1'];

function unique(array) {

    var res = [];

    for (var i = 0, len = array.length; i < len; i++) {

        var current = array[i];

        if (res.indexOf(current) === -1) {

            res.push(current)

        }

    }

    return res;

}

console.log(unique(array));

//ES6 SET

var array = [1, 2, 1, 1, '1'];

function unique(array) {

  return Array.from(new Set(array));

}

console.log(unique(array)); // [1, 2, "1"]

5、实现继承

//原型链继承

function Cat(){

}

Cat.prototype = new Animal();

Cat.prototype.name = 'cat';

var cat = new Cat();

console.log(cat.name);

//构造继承

function Cat(name){

  Animal.call(this);

  this.name = name || 'Tom';

}

var cat = new Cat();

console.log(cat.name);

//拷贝继承

function Cat(name){

  var animal = new Animal();

  for(var p in animal){

    Cat.prototype[p] = animal[p];

  }

  Cat.prototype.name = name || 'Tom';

}

var cat = new Cat();

console.log(cat.name);

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>冒泡排序</title>

</head>

<body>

<script>

    //思路:先比较一轮一次,然后用for循环比较一轮多次,然后再加for循环比较多轮多次

    //从大到小排序

    var array=[10,20,9,8,79,65,100];

    //比较轮数

    for ( var i=0;i<array.length-1;i++){

        //每轮比较次数,次数=长度-1-此时的轮数

        for (var j=0;j<array.length-1-i;j++) {

            if (array[j] > array[j + 1]) {

                var temp = array[i];

                array[j] = array[j + 1];

                array[j + 1] = temp;

            } //end if

        }//end for 次数

    } //end for 轮数

    console

上一篇 下一篇

猜你喜欢

热点阅读