前端面试题2
1. Persistence
Write a function, persistence, that takes in a positive parameter num and returns its multiplicative persistence,which is the number of times you must multiply the digits in num until you reach a single digit.
For example:
Because 39: 3*9 = 27, 2*7 = 14, 1*4=4,and 4 has only one digit, persistence(39) === 3;
Because 999: 9*9*9 = 729, 7*2*9 = 126, 1*2*6 = 12, and finally 1*2 = 2, persistence(999) === 4;
Because 4: 4 is already a one-digit number, persistence(4) === 0;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function persistence(num) {
if (Object.prototype.toString.call(num) === '[object Number]' && num > 0) {
var times = 0;
if (num.toString().length === 1) return times;
return getTimesOfCaculatingASingleProductRecursively(num, times);
} else {
throw 'Please input a positive decimal Number!';
}
function getTimesOfCaculatingASingleProductRecursively(num, times) {
var digitsArray = num.toString().split('');
var product = digitsArray.reduce(function(current, next) {
return current * next;
});
times++;
if (product.toString().length === 1) {
return times;
} else {
return getTimesOfCaculatingASingleProductRecursively(product, times);
}
}
}
console.log(persistence(39)); //3
console.log(persistence(999)); //4
console.log(persistence(4)); //0
console.log(persistence('Jeff')); //Exception: 'Please input a decimal Number!' Check the error in console.
2. Parse Object
Write a function parse to object that meets following result:
parse(object, ‘b.c’) == 4); //true
parse(object, ‘d[0].e’) == 5); //true
parse(object, ‘d.0.e’) == 5); //true
parse(object, ‘d[1].e[g]’) == 6); //true
parse(object, ‘d.1.e.g’) == 6); //true
parse(object, ‘f’) == ‘undefined’); //true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
let object = {
b: { c: 4 },
d: [
{ e: 5 },
{
e: {
g: 6
}
}
]
};
function parse(obj, parseStr) {
try {
if (parseStr.length == 0) throw 'Please input a key string!';
var keyList = parseStr
.replace(/\]|\[|\./g, ' ')
.split(' ')
.filter(c => c.length > 0);
var value = 'undefined';
for (let i = 0; i <= keyList.length; i++) {
var curKey = keyList[i];
if (!curKey || (i == 0 && !obj.hasOwnProperty(curKey))) break;
if (i == 0) value = obj[curKey];
else value = value[curKey];
i += 1;
var nextKey = keyList[i];
if (value && value.hasOwnProperty(nextKey)) {
value = value[nextKey];
}
}
return value;
} catch (error) {
console.error(error);
}
}
console.log(parse(object, 'b.c') == 4); //true
console.log(parse(object, 'd[0].e') == 5); //true
console.log(parse(object, 'd.0.e') == 5); //true
console.log(parse(object, 'd[1].e[g]') == 6); //true
console.log(parse(object, 'd.1.e.g') == 6); //true
console.log(parse(object, 'f') == 'undefined'); //true
3. 数字的千位分割
3.1 使用正则
使用正则实现函数 numSplit,把任意数字转成千位分隔符格式,如 numSplit(12345678901) => 12,345,678,901
1
2
3
4
5
6
function numSplit(num, digits) {
var reg = new RegExp('(\\d)(?=(\\d{' + digits + '})+$)', 'g');
return num.toString().replace(reg, '$1,');
}
console.log(numSplit(12345678901, 3));
3.2 For 循环
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function numSplit(num, digits) {
var arr = num.toString().split('');
var result = [];
for (var i = arr.length - 1; i >= 0; ) {
if (i >= digits) {
for (var j = 0; j < digits; j++) {
result.unshift(arr[i - j]);
}
result.unshift(',');
i -= digits;
} else {
result.unshift(arr[i]);
i--;
}
}
return result.join('');
}
console.log(numSplit(12345678901, 3));
3.3 Recursion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
function numSplit(num, digits) {
var numString = num.toString();
var numStringLen = numString.length;
var result = '';
if (numStringLen > digits) {
result = numSplit(numString.slice(0, numStringLen - digits), digits) + ',' + numString.slice(-digits);
} else {
result += numString;
}
return result;
}
console.log(numSplit(12345678901, 3));
4. 数组去重和排序
请对下列数组去重,并且实现 sort 函数按升序排序 var arr = [12, 12, 9, 20, 6, 31, 24, 6, 15];
4.1 Hash table
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var arr = [12, 12, 9, 20, 6, 31, 24, 6, 15];
function distinctAndAscSort(arr) {
var hash = {};
var result = [];
arr.forEach(a => {
if (!hash[a]) {
hash[a] = true;
result.push(a);
}
});
result.sort(function(c, n) {
return c - n;
});
return result;
}
console.log(distinctAndAscSort(arr));
4.2 More?
5. 实现一个 LazyMan
可以按照以下方式调用
LazyMan(‘Hank’) 输出:
Hi! This is Hank!
LazyMan(‘Hank’).sleep(10).eat(‘dinner’) 输出:
Hi! This is Hank!
//等待 10 秒..
Wake up after 10
Eat dinner~
LazyMan(‘Hank’).eat(‘dinner’).eat(‘supper’) 输出:
Hi! This is Hank!
Eat dinner~
Eat supper~
LazyMan(‘Hank’).sleepFirst(5).eat(‘supper’) 输出:
//等待 5 秒
Wake up after 5
Hi! This is Hank!
Eat supper~
以此类推
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
function _LazyMan(name) {
var self = this;
self.tasks = [];
self.tasks.push(function() {
console.log('Hi! This is ' + name);
self.next();
return self;
});
self.next = function() {
var task = self.tasks.shift();
task && task();
return self;
};
self.sleepFirst = function(time) {
self.tasks.unshift(function() {
setTimeout(function() {
console.log('Wake up after ' + time);
self.next();
}, time * 1000);
});
return self;
};
self.sleep = function(time) {
self.tasks.push(function() {
setTimeout(function() {
console.log('Wake up after ' + time);
self.next();
}, time * 1000);
});
return self;
};
self.eat = function(food) {
self.tasks.push(function() {
console.log('Eat ' + food);
self.next();
});
return self;
};
setTimeout(self.next);
}
function LazyMan(name) {
return new _LazyMan(name);
}
//LazyMan('Hank');
// LazyMan('Hank')
// .sleep(10)
// .eat('dinner');
// LazyMan('Hank')
// .eat('dinner')
// .eat('supper');
LazyMan('Hank')
.sleepFirst(5)
.eat('supper');