金s办公软件web前端笔试题

1.

    var arr = [];
    arr['a'] = 1;
    console.log(arr.length); // A
    arr['4'] = 2;
    console.log(arr.length); // B
    arr.length = 0;
    console.log(arr) // C

A、B、C分别输出什么?

运行结果如下:

    var arr = [];
    arr['a'] = 1;
    console.log(arr); // [a: 1]
    console.log(arr.length); // 0
    arr['4'] = 2;
    console.log(arr) // (5) [empty × 4, 2, a: 1]
    console.log(arr.length); // 5
    arr.length = 0;
    console.log(arr) // [a: 1]
    console.log(arr.length); // 0

所以A为0,B为5,C为[a:1]

2.

for(var i=0; i < 5; i ++) {
    // 在此处编写代码
    // 每隔一秒按顺序输出i值
}

解法:

    for (var i = 0; i < 5; i++) {
        // 在此处编写代码
        // 每隔一秒按顺序输出i值
        (function(i) {
            setTimeout(() => {
                console.log(i)
            }, 1000 * i)
        })(i)
    }

这道题如果没有限定给出给定的代码,还可以根据ES6块级作用域的知识把for循环中的var改成let,或者用Promise

    var arr = []
    var output = (i) => new Promise(resolve => {
        setTimeout(() => {
            console.log(i);
            resolve()
        }, 1000 * i)
    });
    for (var i = 0; i < 5; i++) {
        arr.push(output(i))
    };

3.有如下代码:

    var f = function g() {
        return 23;
    };
    typeof g()

运行结果是:

报错

(扩展:如果题目中typeof f === 'function', typeof f() === 'number')

4.有如下代码:

    function showCase(value) {
        switch (value) {
            case 'A':
                console.log(1);
                break;
            case 'string':
                console.log(2);
                break;
            case undefined:
                console.log(3);
                break;
            case 'undefined':
                console.log(4);
                break;
            default:
                console.log(5)
        }
    }
    showCase(new String('A'))

运行结果是:

5

(扩展:console.log(new String('A')) => String {"A"})

5.请用JavaScript实现map的数据结构,要求数据只能通过map提供的接口进行访问。

解析:

map的数据结构方法有

属性/方法              作用
size属性               size属性返回 Map 结构的成员总数。
set(key, value)       set方法设置键名key对应的键值为value,然后返回整个 Map 结构。如果key已经有值,则键值会被更新,否则就新生成该键。set方法返回的是当前的Map对象,因此可以采用链式写法。
get(key)              get方法读取key对应的键值,如果找不到key,返回undefined。
has(key)              has方法返回一个布尔值,表示某个键是否在当前 Map 对象之中。
delete(key)           delete方法删除某个键,返回true。如果删除失败,返回false。
clear()               clear方法清除所有成员,没有返回值。

参考:

    function MyMap() {
        this.map = new Object();
        this.length = 0;

        this.size = function() {
            return this.length;
        }

        this.set = function(key, value) {
            if (!this.map[key]) {
                ++this.length;
            }
            this.map[key] = value;
        }
        this.get = function(key) {
            return this.map[key] ? this.map[key] : undefined;
        }
        this.has = function(key) {
            return this.map[key] ? true : false;
        }
        this.delete = function(key) {
            if (this.map[key]) {
                --this.length;
                delete this.map[key];
                return true;
            } else {
                return false;
            }
        }

        this.clear = function() {
            this.map = new Object();
            this.length = 0;
        }

    }

6.给定一个排好序的整数数组,判断其中是否存在两个数之和等于指定的值,时间复杂度最好能达到O(n)。(例如:[1,2,3,4,5,9],指定值为12,结果为true)

    var twoSum = function(nums, target) {
        var arr = {};
        for (var i = 0; i < nums.length; i++) {
            if (typeof(arr[nums[i]] !== "undefined")) {
                return true
            } 
            arr[target - nums[i]] = i
        }
    }
原文地址:https://www.cnblogs.com/lhh520/p/10323044.html