zijie
1. 下列输出什么结果
undefined == false; // true undefined === false; // false [] == []; // false [] === []; // false {} == {}; // Uncaught SyntaxError: Unexpected token '==' {} === {}; // Uncaught SyntaxError: Unexpected token '==='
2. 找到字符串中,第一个不重复字符,存在的话,返回这个字符的序号;如果不存在,则返回-1;
/*** 如:* s = leetcode return 0;* s = love leetcode return 2; * s = aabb return -1; */ const str = 'leetcode'; // (.)捕获一个字符 // (?=.*\\1)是一个正向先行断言,表示在当前位置之后存在于捕获组\\1(即前面捕获的字符)相同的字符 // g 标志标识全局匹配,这样可以匹配字符串中所有重复的字符 let reg = new RegExp('(.)(?=.*\\1)', 'g'); // 从原始字符串中去除所有匹配到的重复字符串,得到了不重复的字符 const nonRepeatedChars = str.replace(reg, ''); console.log(nonRepeatedChars); const firstChart = nonRepeatedChars.length > 0 ? nonRepeatedChars[0] : null;
3. 根据数据动态画出dom元素
const data = {tagName: 'ul',props: {'class': 'list'},children: [{tagName: 'li', children: ['douyin']},{tagName: 'li', children: ['toutiao']}] }; <ul class="list"><li>douyin</li><li>toutiao</li> </ul>
const createEl = (item, parentEl) => { const node = document.createElement(item.tagName);if (item.props) {const cls = Object.keys(item.props)[0];node.setAttribute(cls, item.props[cls]);}const childrenList = item?.children;const isString = Array.isArray(childrenList) && childrenList.length > 0 &&typeof childrenList[0] === 'string';if (isString) {node.textContent = childrenList[0];}parentEl.appendChild(node);if (!isString) {childrenList.forEach(child => {createEl(child, node);});} } createEl(data, document.body);
4. 以下代码执行结果
第一道题
async function async1() {console.log("async1");await async2();console.log("async1 end"); } async function async2() {console.log("async2"); } console.log("script start"); setTimeout(() => {console.log("setTimeout"); },0) async1(); new Promise(() => {console.log("promise1"); }).then(() => {console.log("promise2"); }) console.log("script end");输出结果: script start async1 async2 promise1 script endasync1 end promise2 setTimeout
第二道题
console.log('script start'); var asyncFn = async() => {await new Promise((resolve) => {console.log("asyncFn");resolve();}).then(() => {console.log("asyncFn promise1");})console.log("asyncFn end"); }; asyncFn(); setTimeout(function() {console.log("setTimeout"); }, 0); new Promise((resolve) => {console.log("Promise");resolve(); }).then(function(){console.log("promise1"); }).then(function() {console.log("promise2"); }); console.log("script end");输出结果: script start asyncFn Promise script endasyncFn promise1 promise1 asyncFn end promise2 setTimeout
5. 给下列版本排序
var versions = ["1.45.0", "1.5", "1.2.5", "3.3.3.3.3", "6"];
注意:1.45.0小于1.5
meituan
1. 手动实现数组的 flat 方法(ES2019提供的方法)
Array.prototype.myFlat = function(depth = 1) {const result = [];for(const item of this) {if (Array.isArray(item) && depth > 0) {// 递归调用 myFlat 方法,深度减1result.push(...item.myFlat(depth -1));}else {result.push(item);}}return result; } const arr = [1,3,4,[5,6,[7,8]]]; console.log(arr.myFlat(2));
2. npm包版本比较,如果 version1 > version2 返回 1,如果 version1 < version2 返回 -1,其他情况返回0
比如:1.2.3 vs 1.2;
1.2.3 vs 3.4.1
const compareVersion = (version1,version2) => {const firstArr = version1.split('.');const sencondArr = version2.split('.');const length = Math.max(firstArr.length, sencondArr.length); // 取最大的长度for(let i = 0; i < length;i++) {const v1Part = i < firstArr.length ? firstArr[i] : 0;const v2Part = i < sencondArr.length ? sencondArr[i] : 0;if (v1Part > v2Part) {return 1;}else if (v1Part < v2Part) {return -1;}}return 0 } const result = compareVersion('1.2.3','1.2'); console.log(result);
其他常见笔试题
1. 手写一个函数,模拟JS中的new操作符
function myNew(constructor,...args) {const obj = {};obj.__proto__ = constructor.prototype;const result = constructor.apply(obj, args);return typeof result === 'object' && result !== null ? result : obj; } function Person(name,age) {this.name = name;this.age = age; } Person.prototype.sayHello = function() {console.log(`hello, my name is ${this.name} and I'm ${this.age} years old.`); } const person = myNew(Person, 'Alice', 30); person.sayHello();
2. 实现一个函数,将驼峰命名法的字符串转换为下划线命名法
function camelToSnake(str) {const snakeStr = str.replace(/[A-Z]/g, (match) => {return '_' + match.toLowerCase();})return snakeStr.replace(/^_/, ''); } const camelCase = 'HelloWorldJavaScript'; console.log(camelToSnake(camelCase));