有点难度的校招 前端面试题 及个人做的一些答案

/**
 * 考查知识点,闭包,函数调用
 */

function createFunctions() {
    var result = new Array();
    for (var i = 0; i < 5; i++) {
        result[i] = function () {
            return i;
        }
    }
    return result;
}
var funcs = createFunctions();
for (var i = 0; i < funcs.length; i++) {
    console.log(funcs[i]());
}

/**
 * 考查知识点 单进程异步函数
 */

var func = function () {
    console.log('1');
    setTimeout(function () {
        console.log('2')
    }, 0)
    console.log('3');
}
func();

/**
 * 考查知识点 闭包,引用类型对象输出结果
 */

(() => {
    for (var i = 0; i < 10; i++) {
        setInterval(() => {
            var data = { a: 10, b: 20 };
            console.log('第' + i + '条数据:' + data);
        }, 1000);
    }
})()

/**
 * 考查知识点 
 * js代码缩进及实际效果
 */

var i, j;
var a = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
for (i = 0; i < 10; i++)
    for (j = 0; j < i; j++)
        a[i] = a[i] + a[j];
for (i = 0; i < 10; i++)
    console.log(a[i]);
console.log("\n")

/**
 * 输入一个正整数n,判断它是否为一个对称数 例:12321 是 123321 是 1234 不是
 */

function IsSymmetry(n) {
    //判断数字
    if (isNaN(n)) return false;
    //判断正数
    if (n <= 0) return false;
    //计算计算次数
    var str = n.toString();
    var count = str.length / 2;
    console.log(count)
    for (let i = 0; i < count; i++) {
        let strL = str.substr(i, 1);
        let strR = str.substr(str.length - 1 - i, 1);
        if (strL != strR) {
            return false;
        }
    }
    return true;
}
console.log(IsSymmetry(1233121) ? "是对称的,老哥" : "不是对称的,老弟");

/**
 * 把数组倒序输出(不能使用内置方法)
 * var arr=['name','first',5,7,9,6,1]
 */

var arr = ['name', 'first', 5, 7, 9, 6, 1];
var outArr = [];
for (let i = arr.length - 1; i >= 0; i--) {
    outArr.push(arr[i]);
}
console.log(outArr);

/**
 * 数组中的重复项最多出现N次
 * 范例:
 * outputNth([1,1,1,1],2) //return [1,1]
 * outputNth([20,37,20,20,21],2) //return [20,20,37,21]
 * 附加题:
 * outputNth([20,37,20,20,21],2) //return [20,37,20,21]
 */

//答案
function outputNth(arr, n) {
    var arr2 = [];
    for (let i = 0; i < arr.length; i++) {
        let value = arr[i];
        let count = 0;
        for (let j = 0; j < arr2.length; j++) {
            if (arr2[j] == value) count++;
        }
        if (count < n) {
            let position = arr2.indexOf(value);
            if (position == -1)
                arr2.push(value);
            else
                arr2.splice(position + 1, 0, value);
        }
    }
    return arr2;
}
console.log(outputNth([20, 37, 20, 20, 21], 2));

//附加题答案  一个吐槽点:附加题更简单
function outputNth(arr, n) {
    var arr2 = [];
    for (let i = 0; i < arr.length; i++) {
        let value = arr[i];
        let count = 0;
        for (let j = 0; j < arr2.length; j++) {
            if (arr2[j] == value) count++;
        }
        if (count < n) arr2.push(value);
    }
    return arr2;
}
console.log(outputNth([20,37,20,20,21], 2));

/**
 * 设计一个对象,支持push,pop,getMin的调用
 * push(x)  将元素x添加到对象中
 * pop()    获取顶部元素并移除对象顶部的元素
 * getMin() 查找对象中的最小元素
 * 
 * 要求:
 * 不能使用字符串相关函数
 * 不能用-1
 * 不能用长度函数 count length
 * 不能用删除 unset
 * 不能用for in, foreach
 * 不能使用数字的pop push函数
 * 不能使用splice相关函数
 */

/**
 * 创建你的MinStack对象并实例化它
 * var obj = Object.create(MinStack).createNew()
 * obj.push(x);
 * var param_3 = obj.pop();
 * var param_4 = obj.getMin();
 */

//答案
var MinStack = function () {
    this.dataStore = []
    this.top = 0;
}
MinStack.prototype.push = function (element) {
    this.dataStore[this.top++] = element;
}

MinStack.prototype.pop = function () {
    return this.dataStore[--this.top];
}

MinStack.prototype.getMin = function () {
    var min;
    if (this.top == 0) return null;
    else {
        min = this.dataStore[0]
        for (let i = 0; i < this.top; i++) {
            if (this.dataStore[i] < min) {
                min = this.dataStore[i];
            }
        }
        return min;
    }
}

var obj = new MinStack();
obj.push(1);
obj.push(2);
obj.push(3);
console.log(obj);
var param_3 = obj.pop();
var param_4 = obj.getMin();
console.log(param_3)
console.log(param_4)


/**
 * 写出遍历树的代码
 * 考查知识点 递归 类型判断 typeof instanceof 的使用
 */
function eachArr(arr) {
    for (let i = 0; i < arr.length; i++) {
        if (arr[i] instanceof Array)
            eachArr(arr[i]);
        else console.log(arr[i]);
    }
}
eachArr([1, 2, 3, 4, [5, 6, 7, 8, [9, 10, 11, 12]]]);

function addName(obj) {
    obj.name = "a";
    obj = {};
    obj.name = "b";
}
var test = new Object();
addName(test);
console.log(test.name);
//原题
int i,j;
static int a[10]={1,1,1,1,1,1,1,1,1,1};
for(i=0;i<10;i++)
    for(j=0;j<i;j++)
        a[i]=a[i]+a[j];
for(i=0;i<10;i++)
    console.log(a[i]);
console.log("\n")

//改成C++能成功编译后的代码段
int i, j, k;
static int a[10] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
for (i = 0; i < 10; i++) {
    for (j = 0; j < i; j++) {
        a[i] = a[i] + a[j];
    }
    for (k = 0; k < 10; k++) {
        cout << a[k];
        cout << "\n";
    }
}

for (i = 0; i < 10; i++) {
    cout << a[i];
    cout << "\n";
}

结题思路 C++代码
int i, j, k;
static int a[10] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
for (i = 0; i < 10; i++) {
    cout << "第";
    cout << i;
    cout << "轮循环";
    cout << "\n";
    for (j = 0; j < i; j++) {
        cout << "第";
        cout << j;
        cout << "次赋值";
        cout << "\n";
        cout << a[i];
        cout << "+";
        cout << a[j];
        cout << "=";
        a[i] = a[i] + a[j];
        cout << a[i];
        cout << "\n";
    }
    for (k = 0; k < 10; k++) {
        cout << a[k];
        cout << "\n";
    }
}

for (i = 0; i < 10; i++) {
   cout << a[i];
   cout << "\n";
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值