薪资过低欲辞职,面试作题心甚难。javascript
屡屡面试屡遇坎,每日一题快来看。前端
每日工做之余,我会将本身整理的一些前端面试题笔试题整理成每日一题,而后在公众号中推送给你们,天天仅需几分钟作一道题,通过日积月累,在换工做的时候必定能让你拿到一个比较好的offer
。今天这篇文章是我将近期每日一题中比较好的题目及粉丝们分享的一些答案进行的整理,分享给更多的掘友,但愿能够帮助到你。同时关注公众号【前端有的玩】,天天早上八点四十分,准时推送每日一题。java
如今有小编每月老婆给的零花钱清单,可是由于某些缘由,有些月份没有零花钱,以下数据所示git
// 一月,二月, 五月的零花钱
{1:200, 2:140, 5:400}
复制代码
请将上面的数据格式转换为[200, 140, null, null, 400, null, null, null, null, null, null, null]
,其中数组的长度为12
,对应十二个月,请完善下面代码面试
const obj = { 1: 200, 2: 140, 5: 400 };
function translate(obj) {
// 请在此处添加代码
}
// 输出 [200, 140, null, null, 400, null, null, null, null, null, null, null]
console.log(translate(obj));
复制代码
这道题答案能够有许多中,如下罗列了几个群友贡献的答案,为您提供参考编程
const obj = { 1: 200, 2: 140, 5: 400 };
function translate(obj) {
return Array.from({ length: 12 }).map((_, index) => obj[index + 1] || null);
}
// 输出 [200, 140, null, null, 400, null, null, null, null, null, null, null]
console.log(translate(obj));
复制代码
const obj = { 1: 200, 2: 140, 5: 400 };
function translate(obj) {
return Object.assign(Array(13).fill(null), obj).slice(1);
}
// 输出 [200, 140, null, null, 400, null, null, null, null, null, null, null]
console.log(translate(obj));
复制代码
const obj = { 1: 200, 2: 140, 5: 400 };
function translate(obj) {
// 请在此处添加代码
let result = Array(12).fill(null)
Object.entries(obj).forEach(([key, value]) => {
result[key - 1] = value;
});
return result;
}
// 输出 [200, 140, null, null, 400, null, null, null, null, null, null, null]
console.log(translate(obj));
复制代码
请输出1
到400
之间全部数字中包含的1的个数,好比数字1
中包含了一个1
, 数字11
中包含了两个1
,数字20
中不包含1
,数字1
到21
中共包含了13
个1
。数组
function getCount() {
}
// 输出 180
console.log(getCount())
复制代码
这个答案比较经典,性能也算是很不错的了markdown
const sum1s = num => {
let numstr
if (!num) return 0
if (typeof num === 'string') numstr = num
else numstr = String(num)
if (Number(numstr) === 0) return 0
const curr =
numstr[0] > 1
? 10 ** (numstr.length - 1) +
numstr[0] * (numstr.length - 1) * 10 ** (numstr.length - 2)
: sum1s(10 ** (numstr.length - 1) - 1) + 1
return curr + sum1s(numstr.substr(1))
}
// 输出 180
console.log(sum1s(400))
复制代码
这个用到了正则,不过对于长字符串正则可能性能会有点点差函数
function countOne(num){
// num为正整数,方法有点儿暴力
return Array.from({length:num},(v,i)=>i+1).join('').replace(/[^1]/g,'').length
}
console.log(countOne(400))
复制代码
下面这个答案算是中规中矩的答案了,将每个数字转换为字符串而后统计1
的个数性能
function getCount() {
let count = 0
for(let i=1;i<400;i++) {
count = count + `${i}`.split('1').length - 1
}
return count
}
// 输出 180
console.log(getCount())
复制代码
垂帘画阁画帘垂,谁系怀思怀系谁?影弄花枝花弄影,丝牵柳线柳牵丝。这是一首回文诗,即每一句诗正向反向读都是同样的。下面这道题是一道回文数字,即数字正向反向读都是同样的,好比11
,1221
,2112
等等。
请打印出1 - 10000
之间的全部回文数字。其中1~9
由于只有一位数字,因此不算回文数字。
const palindrome = length => {
const res = []
const digits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
const add = (current, length) => {
if (length <= 1) return
digits.forEach(digit => {
res.push(digit + current + digit)
add(digit + current + digit, length - 2)
})
}
digits.forEach(num => {
add(num, length - 1)
res.push(num + num)
add(num + num, length - 2)
})
return res.filter(num => !num.startsWith('0'))
}
// 总共189个
console.log(palindrome(4))
复制代码
function palindrome (max) {
return Array(max + 1).fill('').reduce((a, c, i) => {
if (i > 10) {
const arr = Array.from(`${i}`)
const [x, y] = [`${i}`, arr.reverse().join('')]
x === y && a.push(i)
}
return a
}, [])
}
// 总共189个
console.log(palindrome(10000))
复制代码
const result = [...Array(10000).keys()].filter((x) => x> 10 && x === Number(x.toString().split('').reverse().join('')) )
console.log(result)
复制代码
请实现下面代码中的函数fn
,使其能够输出指定id
对应的全部父id
及其自身id
const data = [
{
id: 1,
name: '222',
children: [{
id: 2,
name: '34',
children: [{
id: 112,
name: '334',
}, {
id: 113,
name: '354',
}
]
}]
}
]
function fn(id) {
}
// 输出 [1, 2, 112]
console.log(fn(112))
复制代码
const data = [
{
id: 1,
name: '222',
children: [{
id: 2,
name: '34',
children: [{
id: 112,
name: '334',
}, {
id: 113,
name: '354',
}
]
}]
}
]
function fn(id) {
const res = []
const find = _ => {
if (!_) return
return _.find(item => (item.id === id || find(item.children)) && res.push(item.id))
}
find(data)
return res.reverse()
}
console.log(fn(112))
复制代码
const fn = (id, ancestors = [], current = data) => {
for (let i = 0; i < current.length; i++) {
if (current[i].id === id) return ancestors.concat(id)
if (current[i].children && current[i].children.length) {
const ret = fn(id, ancestors.concat(current[i].id), current[i].children)
if (ret) return ret
}
}
}
console.log(fn(112))
复制代码
function fn(id) {
const arr = []
const getIds = (ids) => {
for (const v of ids) {
arr.push(v.id)
if (v.id === id) {
return
} else if (v.children) {
getIds(v.children)
} else {
arr.pop()
}
}
}
getIds(data)
return arr
}
console.log(fn(112))
复制代码
请实现函数,将entry
转换为output
的数据格式
const entry = {
'a.b.c.dd': 'abcdd',
'a.d.xx': 'adxx',
'a.e': 'ae'
}
// 要求转换成以下对象
const output = {
a: {
b: {
c: {
dd: 'abcdd'
}
},
d: {
xx: 'adxx'
},
e: 'ae'
}
复制代码
function transform(obj) {
const res = {}
for (let [keys, value] of Object.entries(obj)) {
keys
.split('.')
.reduce((prev, cur, idx, arr) =>
prev[cur] = prev[cur] || (arr[idx + 1] ? {} : value)
, res)
}
return res
}
复制代码
const transform = (input: { [P in string]: string }): Object => {
const ret = {}
Object.entries(input).forEach(([keys, val]) => {
let root = ret
keys.split('.').forEach((key, ind, arr) => {
if (ind === arr.length - 1) root[key] = val
else {
root[key] = root[key] || {}
root = root[key]
}
})
})
return ret
}
复制代码
const entry = {
'a.b.c.dd': 'abcdd',
'a.d.xx': 'adxx',
'a.e': 'ae',
}
const convert = (data) => {
let res = {}
const entries = Object.entries(data)
for (let i = 0; i < entries.length; i++) {
let temp = res
let [key, value] = entries[i]
const everyOne = key.split('.')
for (let j = 0; j < everyOne.length; j++) {
if (j === everyOne.length - 1) {
temp[everyOne[j]] = value
}
temp[everyOne[j]] = temp[everyOne[j]] || {}
temp = temp[everyOne[j]]
}
}
return res
}
console.log(convert(entry))
复制代码
此次整理的这些每日一题都是一些编程题,其中有些仍是咱们平常开发中可能会遇到的问题,经过作这些题也能够检验一下本身对这些实用编程技巧的掌握程度。每日一题来源于公众号【前端有的玩】,工做日天天早上八点四十分准时推送,每日一题,天天成长一点点。
不要吹灭你的灵感和你的想象力; 不要成为你的模型的奴隶。 ——文森特・梵高