During the interview, I selected the answers to one question a day

Time:2021-1-26

The salary is too low to resign, so it’s hard to do the interview.

I’ve been interviewing for many times, so I’ll take a quick look at one question every day.

After days and months multiplying, I will organize some of my front end questions into a daily question, then push them to the official account, and only need a few minutes a day to do a question. After accumulating, you will get a better job when you change your job.offer. Today’s article is to sort out the better questions in the recent daily question and some answers shared by fans, and share them with more friends. I hope it can help you. At the same time, we should pay attention to the official account [front end play], and push the daily question on time every day at eight forty every morning.

Topic 1

subject

Now there is a list of pocket money given by my wife every month, but for some reasons, some months have no pocket money, as shown in the following data

//Pocket money for January, February and may

{1:200, 2:140, 5:400}

Please convert the above data format to[200, 140, null, null, 400, null, null, null, null, null, null, null]Where the length of the array is12, corresponding to 12 months, please complete the following code

const obj = { 1: 200, 2: 140, 5: 400 };

function translate(obj) {

//Please add code here

}

//Output [200, 140, null, null, 400, null, null, null, null, null, null, null, null]

console.log(translate(obj));

answer

There are many answers to this question. The following lists the answers contributed by several group friends for your reference

Answer one
const obj = { 1: 200, 2: 140, 5: 400 };

function translate(obj) {

return Array.from({ length: 12 }).map((_, index) => obj[index + 1] || null);

}

//Output [200, 140, null, null, 400, null, null, null, null, null, null, null, null]

console.log(translate(obj));
Answer two
const obj = { 1: 200, 2: 140, 5: 400 };

function translate(obj) {

return Object.assign(Array(13).fill(null), obj).slice(1);

}

//Output [200, 140, null, null, 400, null, null, null, null, null, null, null, null]

console.log(translate(obj));
Answer three
const obj = { 1: 200, 2: 140, 5: 400 };

function translate(obj) {

//Please add code here

let result = Array(12).fill(null)

Object.entries(obj).forEach(([key, value]) => {

result[key - 1] = value;

});

return result;

}

//Output [200, 140, null, null, 400, null, null, null, null, null, null, null, null]

console.log(translate(obj));

Topic 2

subject

Please output1reach400The number of 1 contained in all numbers between, such as numbers1It contains a1, number11There are two1, number20Not included in1, number1reach21The Chinese Communist Party includes13individual1

function getCount() {

}

//Output 180

console.log(getCount())

answer

Answer one

The answer is classic, and the performance is very good

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

}

//Output 180

console.log(sum1s(400))
Answer two

This uses regular, but for long strings, regular performance may be a bit poor

function countOne(num){

//Num is a positive integer, the method is a bit violent

return Array.from({length:num},(v,i)=>i+1).join('').replace(/[^1]/g,'').length

}

console.log(countOne(400))
Answer three

The following is the right answer. Convert each number into a string and count it1The number of

function getCount() {

let count = 0

for(let i=1;i<400;i++) {

count = count + `${i}`.split('1').length - 1

}

return count

}

//Output 180

console.log(getCount())

Topic 3

Who is nostalgic? Who is nostalgic? The shadow makes the flower branch, the flower makes the shadow, the silk pulls the willow line, the willow pulls the silk. This is a palindrome poem, that is to say, every poem is read in the same way. The following question is a palindrome number, that is, the numbers read forward and backward are the same, for example11,1221,2112wait.

subject

Please print it out1 - 10000All palindrome numbers between. among1~9Because there is only one digit, it is not palindrome.

answer

Answer one
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 in total

console.log(palindrome(4))
Answer two
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 in total

console.log(palindrome(10000))
Answer three

const result = [...Array(10000).keys()].filter((x) => x> 10 && x === Number(x.toString().split('').reverse().join('')) )

console.log(result)

Topic 4

subject

Please implement the function in the following codefnTo enable it to output the specifiedidCorresponding to all parentsidAnd itselfid

const data = [

{

id: 1,

name: '222',

children: [{

id: 2,

name: '34',

children: [{

id: 112,

name: '334',

}, {

id: 113,

name: '354',

}

]

}]

}

]

function fn(id) {

}

//Output [1, 2, 112]

console.log(fn(112))

answer

Answer one

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))
Answer two

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))
Answer three

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

Topic 5

subject

Please implement the function toentryConvert tooutputData format for

const entry = {

'a.b.c.dd': 'abcdd',

'a.d.xx': 'adxx',

'a.e': 'ae'

}

//Request to be converted to the following object

const output = {

a: {

b: {

c: {

dd: 'abcdd'

}

},

d: {

xx: 'adxx'

},

e: 'ae'

}

answer

Answer one

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

}
Answer two

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

}
Answer three

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

summary

These daily problems are some programming problems, some of which may be encountered in our daily development. By doing these problems, you can also test your mastery of these practical programming skills. The daily one comes from the official account [front end play], and the working day is pushed on time every day at eight forty every morning, every day, every day, growing a little bit every day.

epilogue

Don’t blow out your inspiration and imagination; don’t be a slave to your model. ——Vincent Van Gogh