https://img.haomeiwen.com/i5982160/dce35fa31de0a569
//评测题目: 无
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){
2 let index1 = 0,index2 = 0;
3 let result = [];
4 while(index1 !== arr1.length && index2 !== arr2.length){
5 if(arr1[index1] <= arr2[index2]){
6 result.push(arr1[index1]);
7 index1++;
8 }else{
9 result.push(arr2[index2]);
10 index2++;
11 }
12 }
13 if(index1 === arr1.length){
14 result = result.concat(arr2.slice(index2));
15 }else{
16 result = result.concat(arr1.slice(index1));
17 }
18
19 return result;
20 }
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);
网友评论