自我介绍,项目介绍,我把项目功能大致说了一下,说了说其中的优化亮点
看你也有学java,为什么选择前端(前端学得更久,更熟悉一些)
编程题Ⅰ:数据分组
[
{name:"apple", price:"100"},
{name:"apple", price:"200"},
{name:"watermelon", price:"300"}
]
分组后:
{
apple: [
{name:"apple", price:"100"},
{name:"apple", price:"200"},
],
watermelon: [
{name:"watermelon", price:"300"}
]
}
解:
function group(objArr, groupBy) {
const result = {}
for(let i of objArr) {
const groupKey = groupBy(i)
if(result[groupKey]) {
result[groupKey].push(i)
}else {
result[groupKey] = [i]
}
}
return result
}
//test
const objArr = [
{
name:"小明",
age:18,
address:"上海"
},{
name:"小红",
age:18,
address:"北京"
},{
name:"小王",
age:20,
address:"上海"
},{
name:"小芳",
age:19,
address:"北京"
},{
name:"小绿",
age:18,
address:"上海"
},{
name:"小张",
age:19,
address:"上海"
},
]
console.log(group(objArr, item => `${item.age}-${item.address}`))
这个答案面试官一开始还没看出来,正说让我再好好看看题,就ac了
然后问了我一下用reduce做有思路吗,我回答给reduce起始值传入一个空对象,每次遍历时查看空对象中是否存在cur.name,不存在的话创建一个数组[cur]
,将acc[cur.name] = [cur]
,如果存在的话直接acc[cur.name].push(cur),最后返回的就是答案
编程题Ⅱ:我做复杂了,只过了一个用例,面试官说就到这里吧
字符串权重差值最小
“100 200 110”
三个数字三位分别相加,权重分别为1 2 2
相减最小为2-2
输出:[2, 1, 200], [2, 2, 110] 分别为:[权重, 下标, 原始数字]
结束之后我自己又做了一遍:
function accMinMinus(str) {
const strArr = str.split(" ")
const sortedArr = strArr.map((item, index) => {
let sum = item.split("").map(Number).reduce((acc, cur) => acc + cur, 0)
return [sum, index, parseInt(item)]
}).sort((a, b) => a[0] - b[0])
let minSum = Number.MAX_SAFE_INTEGER, result
sortedArr.reduce((acc, cur) => {
const diff = Math.abs(acc[0] - cur[0])
if(diff < minSum) {
minSum = diff
result = [acc, cur]
}
return cur
})
return result
}
//test
const str = "100 200 110"
console.log(accMinMinus(str));
反问:面试周期(2+hr 一周结束,面完一天内给反馈)
途虎进度很快,前前后后半个多月就OC了
#面经##途虎养车##途虎养车校招#