Skip to content

Instantly share code, notes, and snippets.

@hjkcai
Last active October 28, 2016 15:53
Show Gist options
  • Save hjkcai/23ae9dbf76415f971500d93e5b76c9fd to your computer and use it in GitHub Desktop.
Save hjkcai/23ae9dbf76415f971500d93e5b76c9fd to your computer and use it in GitHub Desktop.

深大官微 - 技术部 - JavaScript笔试题

充满套路的一份试卷

1. 填空题

  1. Array.prototype.splice 函数会改变原数组(正确或错误)

  2. 下面代码可以输出 0 ~ 9(正确或错误)

    for (var i = 0; i < 10; i++) {
      console.log(i)
    }
  3. 定义一个函数必须要有名称(正确或错误)

  4. Array.prototype.map 第一个参数是一个函数(正确或错误)

  5. 通过一个函数创建对象需要用____________关键字

  6. StringArray 的原型都指向____________的原型

2. 填写下面表达式的计算结果

  1. var a = 1

  2. undefined == null

  3. NaN === NaN

  4. { value: 1 } === { value: 1 }

  5. [298, 30, 310, 32].sort()

  6. 0.8 - 0.6 == 0.2

  7. Array.isArray(Array.prototype)

  8. 1 < 2 < 3 && 3 < 2 < 1

  9. 1 / 0

  10. ('5' + 3) + ('5' - 3)

3. 填写下面代码段的输出结果

  1. foo 函数的执行结果为:

    function foo () {
      var x = 1
    
      function bar () {
        var y = 'A'
        console.log(y)
      }
    
      console.log(x)
    }
  2. 下面代码的执行结果为:

    var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    console.log(arr.reduce(function (a, b) { return a + b }))
  3. 下面代码的执行结果为:

    function getAge () {
      var y = new Date().getFullYear()
      return y - this.birth
    }
    
    var sixgod = {
      name: '66',
      birth: 1996,
      age: getAge
    }
    
    var f = sixgod.age
    console.log(f())
  4. 下面代码的执行结果为:

    var name = 'World!';
    (function () {
      if (typeof name === 'undefined') {
        var name = 'Jack'
        console.log('Goodbye ' + name)
      } else {
        console.log('Hello ' + name)
      }
    })()
  5. 下面代码的执行结果为:

    function showCase (value) {
      if (value === 'A') console.log('Case A')
      else if (value === 'B') console.log('Case B')
      else if (value == null) console.log('Nothing')
      else console.log('Do not know!')
    }
    
    showCase(new String('A'))
  6. 下面代码的执行结果为:

    var val = 'smtg'
    console.log('Value is ' + (val === 'smtg') ? 'Something' : 'Nothing')
  7. x 的值和输出的内容分别为:

    var x
    if (x = '' == 0 === false) console.log(233)
    else console.log(666)
  8. result 的值为:

    var result = [
      ['keyword', ''],
      ['category', 'all'],
      ['start', '2016-10-28'],
      ['end', '']
    ].filter(function (item) { return item[1] })
    .concat([['page', 2]])
    .map(function (item) { return item[0] + '=' + item[1] })
    .join('&')

4. 程序设计题

  1. DNA匹配

    • 题目描述

      设计一个函数,参数是一个DNA双链的其中一条链(String 类型,如'ATCGGTACGA'),要求返回其匹配的DNA单链(String

    • 样例输入

      'ATTCCGATG'
    • 样例输出

      'TAAGGCTAC'
  2. 字符串拓展

    • 题目描述

      设计一个函数,参数是一个字符串,按照下面的格式返回字符串

    • 样例输入

      'sixgod'
      'iszu'
    • 样例输出

      'S-Ii-Xxx-Gggg-Ooooo-Dddddd'
      'I-Ss-Zzz-Uuuu'

5. 前端

  1. 下面代码有什么效果?

    var items = document.getElementsByClassName('item')
    items.forEach(function (el) { el.style.color = 'red' })
  2. 下面代码是要实现点击 #bar 后,#foo 中的文本变成“baz”,但有一些错误,请修改:

    <html>
      <head>
        <script src="test.js"></script>
      </head>
      <body>
        <p id="foo"></p>
        <button id="bar">Click!</button>
      </body>
    </html>
    // test.js
    function barClickHandler () {
      document.getElementById('foo').innerText = 'bar'
    }
    
    document.getElementById('bar').addEventListener('click', barClickHandler())
  3. 假设已有ID为 el 的一个 div,请写出向 el 内部的末尾添加一个指向 www.baidu.com 的超链接

  4. 同上题,现在要在 el 的前面(与 el 同级)增加这个链接,请写出相应代码:

  5. 请写出使用原生 XMLHttpRequest 方法 GET https://iszu.cn/board/api/article/333333 (某个具体函数名忘了写近似的就行)

5. 后端

  1. 请写出使用 npm 安装 koa 2.0,并写入依赖的命令行

  2. 下面代码的运行结果为:

    module.exports.foo = '123'
    module.exports = '233'
    console.log(exports.foo)
  3. 先执行 foo.js,则下面代码的运行结果为:

    // foo.js
    var bar = require('./bar')
    module.exports = bar.baz('123')
    // bar.js
    module.exports = {
      baz: function (s) {
        setTimeout(function () {
          var foo = require('./foo')
          console.log(foo)
        }, 1000)
    
        return s + '456'
      }
    }
  4. 假设 foo 文件中的内容为 foobar 文件中的内容为 barbaz 文件中的内容为 baz,下面代码的运行结果为:

    var fs = require('fs')
    var foo
    var bar = fs.readFileSync('bar')
    var baz = fs.readFile('baz')
    
    fs.readFile('foo', function (err, data) {
      foo = data
    })
    
    console.log(foo, bar, baz)
  5. 请使用 http 模块创建一个在 80 端口的服务器,不管请求什么都返回 Hello World!

@jas0ncn
Copy link

jas0ncn commented Oct 28, 2016

1. 填空题

  1. 正确
  2. 正确
  3. 错误
  4. 正确
  5. new
  6. Object

@jas0ncn
Copy link

jas0ncn commented Oct 28, 2016

2. 填写下面表达式的计算结果

  1. undefined
  2. true
  3. false
  4. false
  5. [298, 30, 310, 32]
  6. false
  7. true
  8. true
  9. Infinity
  10. '532'

@jas0ncn
Copy link

jas0ncn commented Oct 28, 2016

3. 填写下面代码段的输出结果

  1. 1
  2. 55
  3. NaN
  4. 'Goodbye Jack'
  5. 'Do not know!'
  6. 'Something'
  7. xfalse, 输出为666
  8. 'category=all&start=2016-10-28&page=2'

@jas0ncn
Copy link

jas0ncn commented Oct 28, 2016

4. 程序设计题

   function dnaChain (dna) {
     const list = {A: 'T', T: 'A', G: 'C', C: 'G'}
     return dna.split('').map(v => list[v]).join('')
   }
function stringExtend (s) {
  return s.split('')
          .map((v, i) => Array(i + 1).fill(v).join('').replace(/\w/, _ => _.toUpperCase()))
          .join('-')
}

@jas0ncn
Copy link

jas0ncn commented Oct 28, 2016

5. 前端

  1. 没有什么效果,因为 document.getElementsByClassName('item') 返回的是一个 HTML Collection,没有 forEach 方法,故程序其实是会报错的。

  2. 代码如下:

    <html>
      <head>
      </head>
      <body>
         <p id="foo"></p>
         <button id="bar">Click!</button>
         <script src="test.js"></script>
      </body>
    </html>

    test.js 代码如下:

    function barClickHandler () {
      document.getElementById('foo').innerText = 'bar'
    }
    
    document.getElementById('bar').addEventListener('click', barClickHandler)
  3. 代码如下:

    let a = document.createElement('a')
    a.href = 'http://baidu.com'
    a.innerText = '百度'
    document.getElementById('el').appendChild(a)
  4. 代码如下:

    let el = document.getElementById('el')
    el.parentElement.insertBefore(a, el)
  5. 代码如下:

    const xhr = new XMLHttpRequest();
    xhr.open('GET', 'https://iszu.cn/board/api/article/333333',  true)
    // 异步请求回调
    xhr.onreadystatechange = function(){
       if(xhr.readyState === 4){
          console.log(xhr.responseText)
       }
    }
    xhr.send()

@jas0ncn
Copy link

jas0ncn commented Oct 28, 2016

5. 后端

  1. 代码如下:

    npm install koa@2 --save

    或简写:

    npm i koa@2 -S
  2. 123

  3. 123456

  4. undefined, bar, undefined

  5. 代码如下:

    const http = require('http')
    http.createServer((req, res) => {
       res.end('Hello World!')
    }).listen(80)

@Dreamacro
Copy link

4. 程序设计题

给楼上补充一下
1.

function dnaChain (dna) {
    const pair = { A: 'T', T: 'A', G: 'C', C: 'G' }
    return dna.split('').map(_ => pair[_]).join('')
}
function stringExtend (s) {
    return s.split('')
            .map((v, i) => Array(i + 1).fill(v).join('').replace(/\w/, _ => _.toUpperCase()))
            .join('-')
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment