13518219792

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

如果我去参加前端面试,我应该能做出大圣老师的这道题...

本文转载自微信公众号「Piper蛋窝」,作者Piper蛋。转载本文请联系Piper蛋窝公众号。

成都创新互联公司专注于红旗企业网站建设,成都响应式网站建设公司,商城开发。红旗网站建设公司,为红旗等地区提供建站服务。全流程按需定制制作,专业设计,全程项目跟踪,成都创新互联公司专业和态度为您提供的服务

我是一名自学敲代码的管理学研究生,喜欢 js/ts 但是菜得不行,平常挺关注国内的前端圈。

除了读雪碧大佬[1]等等大佬的知乎外(蒟蒻还看不太懂),平常也看看大圣老师[2]等等的B站。

有一次看大圣老师直播点评简历,他提到:“如果我来面试你,我就把我面前的笔记本给你,随便给你打开个网页比如淘宝,你给我用浏览器现场统计一下各个标签出现的次数。”

!这道题应该不难?我分析无非就是考察了三点:

刚和爸妈打完球回来,那我就做做这道题。

首先咱捋一下思路:

然后我们捋一下需要哪些技术细节:

值得一提的是,我近一个月里写了基于 C++ 、Python 、 JavaScript/TypeScript 、 Scala/Java 的不同项目/小脚本(工作要求...),所以我也记不住 JavaScript 的 API ,我都是在浏览器控制台里试出来的,比如 获取标签的名字是 tagName 、 获取子节点 Array 是 children 。如下图,我试关键词试出来的,要不然谁记得住啊。

输入 tag 会不会得到我想要的 API 呢?果然!

下面动手来做吧

第零步,打开浏览器的 Sources ,新建一个 Snippet 。

Sources

首先我不知道 JavaScript 里有没有现成的队列数据结构,应该是没有,那我就自己实现一个吧。

 
 
 
 
  1. class Queue { 
  2.     #array = [] 
  3.     constructor () { 
  4.         this.#array = [] 
  5.     } 
  6.  
  7.     top () { 
  8.         return this.#array[0] 
  9.     } 
  10.  
  11.     size () { 
  12.         return this.#array.length 
  13.     } 
  14.  
  15.     pop () { 
  16.         this.#array.shift() 
  17.     } 
  18.  
  19.     push (ele) { 
  20.         this.#array.push(ele) 
  21.     } 

很简单的封装!我平时做算法题都是用 C++ ,所以这里方法的名称就都尽量接近 C++ 的 std::queue 。

接下来咱们写 BFS 就行了!

我看现在大佬们都把每个逻辑封装在函数里,所以咱也把脚本运行逻辑 main() 里,然后再在外面调用一下 main() ,看着整洁点。

 
 
 
 
  1. const main = () => { 
  2.  
  3.     const dict = {} 
  4.     const queue = new Queue() 
  5.  
  6.     const htmlTag = document.children[0] 
  7.     dict[htmlTag.tagName] += 1  // !!! 
  8.     queue.push(htmlTag) 
  9.  
  10.     while (queue.size() > 0) { 
  11.         const t = queue.top() 
  12.         queue.pop() 
  13.  
  14.         for (let i = 0; i < t.children.length; i ++) { 
  15.             childTag = t.children[i] 
  16.             dict[htmlTag.tagName] += 1  // !!! 
  17.             queue.push(childTag) 
  18.         } 
  19.     } 
  20.  
  21.     for (let item in dict) { 
  22.         console.log(item, ': ', dict[item]) 
  23.     } 
  24.  
  25. main() 

上面是最最简单的 BFS 实现了,可见这道题着实不是用算法难为我们,很实在的一道题。

注意我标注的 !!! 两行,这里有一个问题:

咱们把这个逻辑写成一个 Effect ,返回一个函数,以显示咱很注重逻辑复用性(划去)。

 
 
 
 
  1. const addDictEffect =  (dict) => { 
  2.     return (name) => { 
  3.         if (dict[name]) { 
  4.             dict[name] += 1 
  5.         } else { 
  6.             dict[name] = 1 
  7.         } 
  8.     } 

OK 那下面在修改一下 main ,一共有三处!

 
 
 
 
  1. const main = () => { 
  2.  
  3.     const dict = {} 
  4.     const addDict = addDictEffect(dict)  // 第一处! 
  5.     const queue = new Queue() 
  6.  
  7.     const htmlTag = document.children[0] 
  8.     addDict(htmlTag.tagName)  // 第二处! 
  9.     queue.push(htmlTag) 
  10.  
  11.     while (queue.size() > 0) { 
  12.         const t = queue.top() 
  13.         queue.pop() 
  14.  
  15.         for (let i = 0; i < t.children.length; i ++) { 
  16.             childTag = t.children[i] 
  17.             addDict(childTag.tagName)  // 第三处! 
  18.             queue.push(childTag) 
  19.         } 
  20.     } 
  21.  
  22.     for (let item in dict) { 
  23.         console.log(item, ': ', dict[item]) 
  24.     } 
  25.  
  26. main() 

啪!很快啊,本题目解决。www.taobao.com 结果如下。

代码

结果

其他网页均可测试。

完整代码

 
 
 
 
  1. class Queue { 
  2.     #array = [] 
  3.     constructor () { 
  4.         this.#array = [] 
  5.     } 
  6.  
  7.     top () { 
  8.         return this.#array[0] 
  9.     } 
  10.  
  11.     size () { 
  12.         return this.#array.length 
  13.     } 
  14.  
  15.     pop () { 
  16.         this.#array.shift() 
  17.     } 
  18.  
  19.     push (ele) { 
  20.         this.#array.push(ele) 
  21.     } 
  22.  
  23. const addDictEffect =  (dict) => { 
  24.     return (name) => { 
  25.         if (dict[name]) { 
  26.             dict[name] += 1 
  27.         } else { 
  28.             dict[name] = 1 
  29.         } 
  30.     } 
  31.  
  32. const main = () => { 
  33.  
  34.     const dict = {} 
  35.     const addDict = addDictEffect(dict) 
  36.     const queue = new Queue() 
  37.  
  38.     const htmlTag = document.children[0] 
  39.     addDict(htmlTag.tagName) 
  40.     queue.push(htmlTag) 
  41.  
  42.     while (queue.size() > 0) { 
  43.         const t = queue.top() 
  44.         queue.pop() 
  45.  
  46.         for (let i = 0; i < t.children.length; i ++) { 
  47.             childTag = t.children[i] 
  48.             addDict(childTag.tagName) 
  49.             queue.push(childTag) 
  50.         } 
  51.     } 
  52.  
  53.     for (let item in dict) { 
  54.         console.log(item, ': ', dict[item]) 
  55.     } 
  56.  
  57. main() 

目前不会js/ts+没做过项目,菜到都没法给大圣老师发简历让他点评。期待早日能到发简历的地步。


网页名称:如果我去参加前端面试,我应该能做出大圣老师的这道题...
文章起源:http://cdbrznjsb.com/article/cojhsii.html

其他资讯

让你的专属顾问为你服务