search
HomeWeb Front-endJS TutorialJS interview questions that more than 80% of interviewees fail
JS interview questions that more than 80% of interviewees failJun 28, 2020 am 09:57 AM
javascriptfront endinterview

There are 5024 words in total. It takes 6 minutes to finish reading and 2 minutes to speed read. This article was first published in Zhihu column front-end weekly. As written before, the author has interviewed hundreds of front-end engineers during more than 2 years as an interviewer. I was surprised to find that more than 80% of the candidates did not even pass the answer to the following question. What kind of magical interview question is this? What abilities does he examine in candidates? What implications does it have for you who are reading this article? Let me tell you slowly (Related recommendation " Front-end Interview Questions")

Humble Beginning

Recruiting front-end engineers, especially mid-to-senior front-end Engineers, a solid JS foundation is absolutely necessary. Engineers with a weak foundation will most likely be helpless when facing various problems in front-end development. When examining the candidate's JS foundation, I often provide the following code and then ask the candidate to analyze the results of its actual operation:

for (var i = 0; i <p>This code is very short, only 7 lines. I think, it can Students reading this probably don’t need me to explain line by line what this code is doing. The results given by candidates when faced with this code are also different. The following is a typical answer: </p>
  • A. 20% of people will quickly scan the code and then give the result: 0,1,2,3,4,5
  • B. 30% of people will take the code and look at it line by line, and then give the result: 5,0,1, 2,3,4;
  • C. 50% of people will take the code and think about it carefully, and then give the result: 5,5,5,5,5,5 ;

As long as you have a correct understanding of the difference between synchronous and asynchronous code, variable scope, closure and other concepts in JS, you will know that the correct answer is C, and the actual output of the code is:

2017-03-18T00:43:45.873Z 5
2017-03-18T00:43:46.866Z 5
2017-03-18T00:43:46.868Z 5
2017-03-18T00:43:46.868Z 5
2017-03-18T00:43:46.868Z 5
2017-03-18T00:43:46.868Z 5

Next I will ask: If we agree that the arrow indicates that there is a 1 second time interval between the two outputs before and after, and the comma indicates that the time interval between the two outputs before and after it can be ignored, How to describe the actual results of running the code? There will be the following two answers:

  • A. 60% of people will describe it as: 5 -> 5 -> 5 -> 5 -> 5, That is, there is a time interval of 1 second between each 5;
  • B. 40% of people will describe it as: 5 -> 5,5,5,5,5, That is, the first 5 is output directly, and after 1 second, 5 5s are output;

This requires the candidate to be very familiar with the timer working mechanism in JS. During the loop execution, it is set almost simultaneously There are 5 timers. Under normal circumstances, these timers will be triggered after 1 second, and the output after the loop is executed immediately. Obviously, the correct description is B.

If you are considered qualified here, only 20 of 100 people will pass the interview. Students who read this can think carefully, did you pass?

Question 1: Closure

If this question is just to test the candidate’s understanding of JS asynchronous code and variable scope, the limitations are too great. Next, I will ask, if The expected output of the code becomes: 5 -> 0,1,2,3,4. How to modify the code? Students who are familiar with closures can quickly give the following solutions:

for (var i = 0; i <p> Cleverly use IIFE (Immediately Invoked Function Expression: a function expression that is declared and executed) to solve the problems caused by closures. It is indeed A good idea, but beginners may not find such code easy to understand. At least when I first started, I thought about it for a while before I really understood it. </p><p>Is there a more intuitive approach? The answer is yes, we just need to make some adjustments to the loop body so that the code responsible for output can get the i value of each loop. how should I do it? Taking advantage of the feature that the parameter passing of the basic type (Primitive Type) in JS is Pass by Value, it is not difficult to transform the following code: </p><pre lang="js" class="">var output = function (i) {
    setTimeout(function() {        
        console.log(new Date, i);
    }, 1000);
};
for (var i = 0; i <p>can give candidates for the above two solutions It can be considered that the basic understanding and application of JS are good, and 10 points can be added to each. Of course, in the actual interview, some candidates gave the following code: </p><pre lang="text" class="">for (let i = 0; i <p> Careful students will find that there is only a very subtle change here, which is to use let in the ES6 block scope (Block Scope) instead. var is used, but the code will report an error when it is actually run, because the i used in the last output does not exist in the scope where it is located, and i only exists inside the loop. </p><p>Students who can think of ES6 features may not answer correctly, but have demonstrated their understanding of ES6. They can add 5 points and continue with the following questions. </p><h2 id="Following-question-ES">Following question 2: ES6</h2><p>Experienced front-end students may be a little impatient after reading this. I have talked so much, and it is all what he knows. Don’t worry, the difficulty of the challenge will be Continue growing. </p><p>接着上文继续追问:如果期望代码的输出变成 <code class="code">0 -> 1 -> 2 -> 3 -> 4 -> 5</code>,并且要求原有的代码块中的循环和两处 console.log 不变,该怎么改造代码?新的需求可以精确的描述为:代码执行时,立即输出 0,之后每隔 1 秒依次输出 1,2,3,4,循环结束后在大概第 5 秒的时候输出 5(这里使用大概,是为了避免钻牛角尖的同学陷进去,因为 JS 中的定时器触发时机有可能是不确定的,具体可参见 How Javascript Timers Work)。</p><p>看到这里,部分同学会给出下面的可行解:</p><pre lang="js" class="">for (var i = 0; i <p>不得不承认,这种做法虽粗暴有效,但是不算是能额外加分的方案。如果把这次的需求抽象为:在系列异步操作完成(每次循环都产生了 1 个异步操作)之后,再做其他的事情,代码该怎么组织?聪明的你是不是想起了什么?对,就是 Promise。</p><p>可能有的同学会问,不就是在控制台输出几个数字么?至于这样杀鸡用牛刀?你要知道,面试官真正想考察的是候选人是否具备某种能力和素质,因为在现代的前端开发中,处理异步的代码随处可见,熟悉和掌握异步操作的流程控制是成为合格开发者的基本功。</p><p>顺着下来,不难给出基于 Promise 的解决方案(既然 Promise 是 ES6 中的新特性,我们的新代码使用 ES6 编写是不是会更好?如果你这么写了,大概率会让面试官心生好感):</p><pre lang="js" class="">const tasks = [];
for (var i = 0; i  {
        tasks.push(new Promise((resolve) => {
            setTimeout(() => {
                console.log(new Date, j);
                resolve();  // 这里一定要 resolve,否则代码不会按预期 work
            }, 1000 * j);   // 定时器的超时时间逐步增加
        }));
    })(i);
}

Promise.all(tasks).then(() => {
    setTimeout(() => {
        console.log(new Date, i);
    }, 1000);   // 注意这里只需要把超时设置为 1 秒
});

相比而言,笔者更倾向于下面这样看起来更简洁的代码,要知道编程风格也是很多面试官重点考察的点,代码阅读时的颗粒度更小,模块化更好,无疑会是加分点。

const tasks = []; // 这里存放异步操作的 Promise
const output = (i) => new Promise((resolve) => {
    setTimeout(() => {
        console.log(new Date, i);
        resolve();
    }, 1000 * i);
});

// 生成全部的异步操作
for (var i = 0; i  {
    setTimeout(() => {
        console.log(new Date, i);
    }, 1000);
});

读到这里的同学,恭喜你,你下次面试遇到类似的问题,至少能拿到 80 分。

我们都知道使用 Promise 处理异步代码比回调机制让代码可读性更高,但是使用 Promise 的问题也很明显,即如果没有处理 Promise 的 reject,会导致错误被丢进黑洞,好在新版的 Chrome 和 Node 7.x 能对未处理的异常给出 Unhandled Rejection Warning,而排查这些错误还需要一些特别的技巧(浏览器、Node.js)。

追问 3:ES7

既然你都看到这里了,那就再坚持 2 分钟,接下来的内容会让你明白你的坚持是值得的。

多数面试官在决定聘用某个候选人之前还需要考察另外一项重要能力,即技术自驱力,直白的说就是候选人像有内部的马达在驱动他,用漂亮的方式解决工程领域的问题,不断的跟随业务和技术变得越来越牛逼,究竟什么是牛逼?建议阅读程序人生的这篇剖析。

回到正题,既然 Promise 已经被拿下,如何使用 ES7 中的 async await 特性来让这段代码变的更简洁?你是否能够根据自己目前掌握的知识给出答案?请在这里暂停 1 分钟,思考下。

下面是笔者给出的参考代码:

// 模拟其他语言中的 sleep,实际上可以是任何异步操作
const sleep = (timeountMS) => new Promise((resolve) => {
    setTimeout(resolve, timeountMS);
});

(async () => {  // 声明即执行的 async 函数表达式
    for (var i = 0; i <h2 id="总结">总结</h2><p>感谢你花时间读到这里,相信你收获的不仅仅是用 JS 精确控制代码输出的各种技巧,更是对于前端工程师的成长期许:扎实的语言基础、与时俱进的能力、强大技术自驱力。</p><p>推荐教程:《<a href="https://www.php.cn/js-tutorial.html" target="_blank">JS教程</a>》</p>

The above is the detailed content of JS interview questions that more than 80% of interviewees fail. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金社区. If there is any infringement, please contact admin@php.cn delete
es6数组怎么去掉重复并且重新排序es6数组怎么去掉重复并且重新排序May 05, 2022 pm 07:08 PM

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

JavaScript的Symbol类型、隐藏属性及全局注册表详解JavaScript的Symbol类型、隐藏属性及全局注册表详解Jun 02, 2022 am 11:50 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

原来利用纯CSS也能实现文字轮播与图片轮播!原来利用纯CSS也能实现文字轮播与图片轮播!Jun 10, 2022 pm 01:00 PM

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

JavaScript对象的构造函数和new操作符(实例详解)JavaScript对象的构造函数和new操作符(实例详解)May 10, 2022 pm 06:16 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

JavaScript面向对象详细解析之属性描述符JavaScript面向对象详细解析之属性描述符May 27, 2022 pm 05:29 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

javascript怎么移除元素点击事件javascript怎么移除元素点击事件Apr 11, 2022 pm 04:51 PM

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

整理总结JavaScript常见的BOM操作整理总结JavaScript常见的BOM操作Jun 01, 2022 am 11:43 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach是es6里的吗foreach是es6里的吗May 05, 2022 pm 05:59 PM

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),