Toc
0 results found
FBB
艾耕公司笔试
2019/03/08 前端 笔试

今天去了艾耕公司面试,做了一套笔试,感觉题目比较经典,记录一下。

  1. 下列语句分别输出什么?
console.log(parseInt('01') + '0' + parseInt('010'))    //1010
console.log(1 === true)   //false
console.log(typeof Aarry)   //function
console.log('0' == false)   //true
console.log(1.toString())   //报错
console.log(1..toString())   //1
  1. 请给出下列的输出
var arr1 = "john".split(""); //['j', 'o', 'h', 'n']
var arr2 = arr1.reverse(); //['n', 'h', 'o', 'j']     reverse会改变原数组
var arr3 = "jones".split(""); //['j', 'o', 'n', 'e', 's']
arr2.push(arr3); //['n', 'h', 'o', 'j', ['j', 'o', 'n', 'e', 's']]
console.log("array 1 : length=" + arr1.length + " last=" + arr1.slice(-1)); //['n', 'h', 'o', 'j']
console.log("array 2 : length=" + arr2.length + " last=" + arr2.slice(-1)); //['n', 'h', 'o', 'j']
  1. 请给出下列的输出
new Promise(resolve => {
  setTimeout(() => {
    resolve("hi");
  }, 1000);
})
  .then(value => {
    console.log("start ", value);
    throw new Error("test error");
  })
  .catch(() => {
    console.log("I catch: ", "test error");
    //console.log(x)    第二个catch就会报错
  })
  .then(() => {
    console.log("arrive here");
  })
  .catch(() => {
    //用于接收第一个catch之后抛出的错误
    console.log("No, I catch:", "anther error");
  })
  .finally(() => {
    console.log("end");
  });
//start hi        resolve('hi')之后调用.then函数执行输出后,抛出异常
//I catch: , test error       接收到抛出的异常执行输出,catch执行之后仍然返回一个promise
//arrive here
//end     finally永远都会执行的函数,ES2018引入标准的
  1. 给出函数的返回结果
function transferArrToStr(arr) {
  const printArr = [];
  let str = "";
  for (var i = 0; i < arr.length; i++) {
    //变量提升的问题,解决方法可以使用let
    printArr[i] = () => arr[i - 1];
  }
  printArr.forEach(item => {
    str += item();
  });
  return str;
}
const hello = ["h", "e", "l", "l", "o"];
transferArrToStr(hello); //'ooooo'
  1. 给出函数的返回结果
function sortIntegers(a, b) {
  return a - b;
}
function computeProduct(unsorted) {
  //该算法实现计算数组中最大三个数的乘积
  var sort_array = unsorted.sort(sortIntegers);
  var product1 = 1;
  var product2 = 1;
  var array_n_element = sort_array.length - 1;
  for (var x = array_n_element; x > array_n_element - 3; x--) {
    product1 = product1 * sort_array[x];
  }
  product2 = sort_array[0] * sort_array[1] * sort_array[array_n_element];
  if (product1 > product2) return product1;
  return product2;
}
var unsorted_array = [-10, 7, 29, 30, 5, -10, -70];
computeProduct(unsorted_array); //21000
  1. 请输出下列结果
const globalVar = "xyz";
(function outerFunc(outerArg) {
  let outerVar = "a";
  (function innerFunc(innerArg) {
    let innerVar = "b";
    console.log(
      "outerArg = " +
        outerArg +
        "\n" +
        "outerVar = " +
        outerVar +
        "\n" +
        "innerArg = " +
        innerArg +
        "\n" +
        "innerVar = " +
        innerVar +
        "\n" +
        "globalVar = " +
        globalVar
    );
  })(456);
})(123);
  1. 使用栈判断大括号是否闭合
function isBalanced(expression) {
  var checkingString = expression;
  var stack = [];
  if (checkingString.length <= 0) return true;
  for (var i = 0; i < checkingString.length; i++) {
    if (checkingString[i] === "{") {
      stack.push(checkingString[i]);
    } else if (checkingString[i] === "}") {
      if (stack.length > 0) {
        stack.pop() === "{";
        continue;
      } else {
        return false;
      }
    }
  }
  if (stack.pop()) return false;
  return true;
}
  1. 实现二分查找法
打赏
支付宝
微信
本文作者:FBB
版权声明:本文首发于FBB的博客,转载请注明出处!