# 面试遇坎，每日一题我精选了这些题目与答案

### 题目一

#### 题目

``````// 一月，二月， 五月的零花钱
{1:200, 2:140, 5:400}

``````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));

### 题目二

#### 题目

``````function getCount() {

}

// 输出 180
console.log(getCount())

#### 答案

##### 答案一

``````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))

##### 答案三

``````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())

### 题目三

#### 答案

##### 答案一
``````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 => {
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)

### 题目四

#### 题目

``````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))

### 题目五

#### 题目

``````const entry = {
'a.b.c.dd': 'abcdd',
'a.e': 'ae'
}

// 要求转换成如下对象
const output = {
a: {
b: {
c: {
dd: 'abcdd'
}
},
d: {
},
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.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))