时隔两年,再次面试,看看自己的水平
废话不说,上代码
第一部分 10个小题
- 第一题,考基本数据类型,typeof 返回值,typeof永远返回字符串
// 第一题
console.log(typeof typeof typeof null); // string
- 第二题,考简单的正则
// 第二题
const t = 'abcdcba'.replace(/c/, 'h');
console.log('t :', t); // abhdcba
- css优先级
<style>
.classA { color:blue; }
.classB { color:red;}
</style>
<p class="classB classA">hello</p>
元素p内的文字最终什么颜色
// red
- 变量提升
// 第四题
var a = 1;
function fn() {
console.log(a);
var a = 2;
}
fn();
// undefined
- 函数参数,变量赋值
// 第五题
var a = [1, 2, 3, 4];
function set(a) {
a = [5, 6, 7, 8];
}
set(a);
console.log(a); // [1, 2, 3, 4]
- this指向,作用域
// 第6题
var name = '123';
var obj = {
name: '456',
getName: function () {
function printName() {
console.log(this.name);
}
printName();
},
};
obj.getName();
// node环境:undefined
// 浏览器端:123
7.考察js的引用类型,跟第7题差不多
var a = {x: 1};
var b = a;
a.x = 2;
a = {n: 1};
console.log(a);
console.log(b);
- 考察函数调用,作用域
// 8.
var count = 10;
function a() {
return count + 10;
}
function b() {
var count = 20;
return a();
}
console.log(b()); // 20
- setTimeout
setTimeout(() => {
setTimeout(() => {
console.log(1);
}, 0);
}, 0);
setTimeout(() => {
console.log(2);
}, 100);
// 1, 2
- css,两个div重叠,取大的外边距
两个 div 边距多少
<div class="a"></div>
<div class="b"></div>
<style>
.a {
width: 100px;
height: 100px;
margin: 10px;
}
.b {
width: 100px;
height: 100px;
margin: 20px;
}
</style>
// 20px
第二部分,数组扁平化
// 题目:
// var arr1 = [1, 2, [3, 4]];
// arr1.flat();
// // [1, 2, 3, 4]
// var arr2 = [1, 2, [3, 4, [5, 6]]];
// arr2.flat();
// // [1, 2, 3, 4, [5, 6]]
// var arr3 = [1, 2, [3, 4, [5, 6]]];
// arr3.flat(2);
// // [1, 2, 3, 4, 5, 6]
// //使用 Infinity,可展开任意深度的嵌套数组
// var arr4 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]];
// arr4.flat(Infinity);
// // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
// Array.prototype.flat = function(depth = 1) {
// }
// ---------------
Array.prototype.myflat = function (dep = 1) {
return this.reduce((accumulator, currentValue) => {
let output;
if (Array.isArray(currentValue) && dep > 0) {
output = currentValue.myflat(--dep);
} else if (Array.isArray(currentValue)) {
output = [currentValue];
} else {
output = currentValue;
}
return accumulator.concat(output);
}, []);
};
var arr2 = [1, 2, [3, 4, [5, 6]]];
const output = arr2.myflat(2);
console.log('arr2 :', arr2);
console.log('output :', output);
// arr2 : [ 1, 2, [ 3, 4, [ 5, 6 ] ] ]
// output : [ 1, 2, 3, 4, 5, 6 ]
第三部分,merge两个有序的数组,not sort
/**
// 合并有序数组
function merge(arr1, arr2){
// TODO
}
const a = [1,3,5,9];
const b = [2,4,6,8];
merge(a, b); // [1,2,3,4,5,6,8,9]
// not sort
*/
// function merge(arr1, arr2) {
// // 1. 判断谁的长度更长,就是用谁来遍历
// const isArr1 = arr1.length >= arr2.length;
// // 2. m是最长的数组
// const m = isArr1 ? arr1 : arr2;
// // 3. 最终输出的结果
// const result = [];
// // 4. forEach循环
// m.forEach((item, index) => {
// let temp;
// // 5. 判断当前遍历的是arr1还是arr2
// if (isArr1) {
// // 5.1 如果是arr1
// // 那么 把arr2的值拿出来,与当前的值进行比对大小
// // 小的放前面,大的放后面
// temp =
// item >= arr2[index] ? [arr2[index], item] : [item, arr2[index]];
// } else {
// temp =
// item >= arr1[index] ? [arr1[index], item] : [item, arr1[index]];
// }
// // 过滤掉为空的的值
// temp = temp.filter((i) => i);
// result.push(...temp);
// });
// return result;
// }
// const a = [1, 3, 5, 92, 100, 200];
// const b = [2, 4, 6, 8, 101, 202];
// const output = merge(a, b);
// console.log('output :', output);
// -----------------
// 优化之后
function merge2(arr1, arr2) {
// 1. 判断谁的长度更长,就是用谁来遍历
const isArr1 = arr1.length >= arr2.length;
// 2. m是最长的数组
const m = isArr1 ? arr1 : arr2;
const mleft = isArr1 ? m.slice(0, arr2.length) : m.slice(0, arr1.length);
const mright = isArr1 ? m.slice(arr2.length) : m.slice(arr1.length);
console.log('mleft :', mleft);
console.log('mright :', mright);
// 3. 最终输出的结果
const result = [];
// 4. forEach循环
mleft.forEach((item, index) => {
let temp;
// 5. 判断当前遍历的是arr1还是arr2
if (isArr1) {
// 5.1 如果是arr1
// 那么 把arr2的值拿出来,与当前的值进行比对大小
// 小的放前面,大的放后面
temp = item >= arr2[index] ? [arr2[index], item] : [item, arr2[index]];
} else {
temp = item >= arr1[index] ? [arr1[index], item] : [item, arr1[index]];
}
result.push(...temp);
});
return result.concat(mright);
}
const a = [1, 3, 5, 92, 100];
const b = [2, 4, 6, 8, 101, 202, 203, 1000];
const output = merge2(a, b);
console.log('output :', output);
// mleft : [ 2, 4, 6, 8, 101 ]
// mright : [ 202, 203, 1000 ]
// output : [
// 1, 2, 3, 4, 5,
// 6, 8, 92, 100, 101,
// 202, 203, 1000
// ]