前端经典笔试题目(随缘更新)

写在前面:

为啥写这个,是因为在找工作,工作真的太难找了。

虽然说大环境不好是一个问题,但是打铁还需自身硬,为此俺要开始努力去刷算法了,把自己笔试的时候遇到的一些题进行分析写一写。

因为本人愚昧,或许解法有些许不妥,还请多多指教,Respect

注意:题目都是按照个人理解写的,抽出其中最重要、关键的信息,不会写原题的(字太多了,我太懒了)。

题目部分:

1.给定一个数组对象resultArray,结构如下:

let resultArray = [
        {
            "pic": "P1",
            "score": [90, 96, 85, 89, 98, 78, 90, 95, 86, 92]
        },
        {
            "pic": "P2",
            "score": [96, 86, 75, 99, 88, 98, 90, 75, 96, 82]
        },
        {
            "pic": "P3",
            "score": [80, 96, 95, 99, 88, 78, 80, 85, 76, 82]
        },
        {
            "pic": "P4",
            "score": [80, 86, 95, 99, 78, 88, 80, 85, 96, 82]
        },
        {
            "pic": "P5",
            "score": [80, 96, 89, 84, 90, 74, 97, 91, 85, 90]
        }
    ]

现有如下要求:

去掉score中的最值,然后计算出每个pic的平均值,并输出如下格式:pn最后得分:98。

本人解法:

let resultArray = [
        {
            "pic": "P1",
            "score": [90, 96, 85, 89, 98, 78, 90, 95, 86, 92]
        },
        {
            "pic": "P2",
            "score": [96, 86, 75, 99, 88, 98, 90, 75, 96, 82]
        },
        {
            "pic": "P3",
            "score": [80, 96, 95, 99, 88, 78, 80, 85, 76, 82]
        },
        {
            "pic": "P4",
            "score": [80, 86, 95, 99, 78, 88, 80, 85, 96, 82]
        },
        {
            "pic": "P5",
            "score": [80, 96, 89, 84, 90, 74, 97, 91, 85, 90]
        }
    ]

    // 遍历数组,对数组进行操作
    resultArray.forEach(item => {
        /**
         * 先使用Math函数提供的最值方法,记住里面的array需要使用展开运算符,要不然会报错,返回值是最大的数
         * 使用indexof获取最值的下标,使用indexof的好处——就算同时存在两个最大/小值也没事,因为indexof默认只取一个
         * 这里还需要注意,如果splice没有设置步长,就会默认去掉后面的数,所以这里设置为1
        */
        item.score.splice(item.score.indexOf(Math.min(...item.score)), 1)
        item.score.splice(item.score.indexOf(Math.max(...item.score)), 1)

        // 求数组的平均数 - foreach是没有返回值的,所以用reduce方法
        let sum = item.score.reduce((item, s, avg) => {
            s += item
            return s
        })
        const avg = sum / item.score.length
        console.log(item.pic, "最后得分:", avg)
    })


2.有以下函数,

let onlyGetone = once(function() {
    console.log('Fire the holl')  
})
onlyGetone()
onlyGetone()
onlyGetone()

在运行之后,输出结果是:

'Fire the holl'

请问:once函数?

本人解法(参考了网上的教程,都说是闭包):

function once(fn) {
    let isFlag = false
    return function() {
        if(!isFlag){
            isFlag = true
            fn() // 这里解法很多,下面就是,但是我并不知道其中的一些作用,感兴趣的可以去尝试看看
            // fn.apply()
            // fn.apply(this)
            // fn.apply(this, arguments)
        }
    }
}


3.如何将“I am developer”,转为“Developer am I”。

秉承着特事特办、投机取巧、倒打一耙的原则,我是这么做的:。

let str = ' I am developer'

// 1.去掉首尾两端的空格
str = str.trim()
// 2.拆分成一个个字符串 - 以空格为分隔符
let arr = str.split(' ')
// 3.developer首字母大写,然后拼接字符串,注意加上空格
// str = arr[2].charAt(0).toUpperCase() + arr[2].slice(1) + ' ' + arr[1] + ' ' + arr[0]

// 3.更优秀的解法 - 使用数组的反转
arr = arr.reverse()
// 4.首字母大写
arr[0] = arr[0].charAt(0).toUpperCase() + arr[0].slice(1)
// 5.拼接成字符串
str = arr.join(' ')
console.log(str)

当然啦,方法千千万万,核心不变就OK了。

不得不说,一些公司面试题用到的一些内置函数确实是比较少见的。

写在最后:

最后的最后,就是最后的最后。

加油吧,骚年。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值