- 实现如图所示的一个A元素,要求如下:
- 垂直居中于页面;
- 随着窗口大小变换,元素与页面的左右边距始终为10px;
- A元素中的字母A水平垂直居中,字号为20px;
- 优先实现高度为宽度的50%,无法实现可实现高度为200px;
-
实现sum函数,使之可满足以下条件:
sum(1,2,3).valueOf(); // 6
sum(2,3)(2).valueOf(); // 6
sum(1)(2)(3)(4).valueOf(); // 10
sum(1)(4,1)(3).valueOf(); // 9 -
实现Function的bind函数,使下面的代码可以打印出’success’
function Animal(name, color) {
this.name = name;
this.color = color;
}
Animal.propotype.say = function() {
console.log('I am a' + this.color + this.name);
}
const Cat = Animal.bind(null, 'cat');
const cat = new Cat('white');
if(cat.say() === 'I am a white cat' && cat instanceof Cat) {
console.log('success');
}
- 给定一个正整数升序排序数组如下:[0,1,2,4,5,7,13,15,16,17],输出区间[‘0->2’, ‘4->5’, ‘7’, ‘13’, ‘15->17’],
function sumaryRanges(arr) {
// your code
}
function sumaryRanges(arr) {
let result = [];
let startI = 0;
let endI = 0;
let startItem = arr[startI];
let item = '';
for(let i=1; i<arr.length-1;i++) {
if(i === arr.length - 1) {
item = startItem + '->' + arr[i];
result.push(item);
}
if(arr[i] !== (startItem + i - startI)) {
endI = i - 1;
if(startI === endI) {
item = startItem + '';
} else {
item = startItem + '->' + arr[endI];
}
result.push(item);
startI = i;
startItem = arr[i];
}
}
return result;
}
- 下面代码的输出是多少?
let a = {};
let b = '123';
let c = 123;
a[b] = 'b';
a[c] = 'c';
console.log(a[b]); // c
// 在 Object 对象类型中,key 值以字符串的形式存在,所以 b、c 在 对象 a 中都是 '123',a[c] 会覆盖 a[b]。
let a = {};
let b = Symbol('123');
let c = Symbol('123');
a[b] = 'b';
a[c] = 'c';
console.log(a[b]); // b
// Symbol 是 ES6 新增的属性,会产生一个唯一值,所以 Symbol('123') 和 Symbol('123') 不相等。
let a = {};
let b = {name: '123'};
let c = {name: '123'};
a[b] = 'b';
a[c] = 'c';
console.log(a[b]); // c
// b 和 c是一个对象,作为 a 的属性出现时,表现为字符串 [Object object],a[c] 会覆盖 a[b]。
- 下面代码的输出是多少?
function Foo() {
Foo.a = function() {
console.log(1);
}
this.a = function() {
console.log(2);
}
}
Foo.a = function() {
console.log(4);
}
Foo.a(); // 4
let obj = new Foo();
obj.a(); // 2
Foo.a(); // 1
- 下面代码的输出顺序:
async function async1() {
console.log(1);
await async2();
console.log(3);
}
async function async2() {
console.log(2);
}
console.log(4);
setTimeout(function() {
console.log(5);
Promise.resolve().then(()=> {
console.log(6);
})
})
setTimeout(function() {
console.log(7);
Promise.resolve().then(()=> {
console.log(8);
})
})
async1();
new Promise(() => {
console.log(10);
})
console.log(11);
- 两个有序数组,合并为一个数组,仍然保持有序,要求时间复杂度为O(n);
- 求斐波那契数列第n项的值,优先递归(避免爆栈)
function fn(current, total, n) {
if(n === 1 || n === 2) return total;
let temp = current;
current = total;
total += current;
--n;
return fn(current, total, n);
}
- 给定一个模板和一个对象,利用对象中的数据渲染模板,并返回最终结果
let template =
'你好,我们公司是{{ company }},我们属于{{group.name}}业务线,我们在招聘各种方向的人才,包括{{group.jobs[0]}}、{{group["jobs"][1]}}等。';
let obj = {
group: {
name: "天猫",
jobs: ["前端"],
},
company: "阿里",
};
function render(template, obj) {
// 代码实现
}
render(template, obj);
- 完成 convert(list) 函数,实现将 list 转为 tree。
/**
* @param list {object[]},
* @param parentKey {string}
* @param currentKey {string}
* @param rootValue {any}
* @return object
*/
function convert(list, parentKey, currentKey, rootValue) {
}
const list = [
{
id: 19,
parentId: 0,
},
{
id: 18,
parentId: 16,
},
{
id: 17,
parentId: 16,
},
{
id: 16,
parentId: 0,
},
];
const result = convert(list, "parentId", "id", 0);
console.log(result);
const tree = {
id: 0,
children: [
{
id: 19,
parentId: 0,
},
{
id: 16,
parentId: 0,
children: [
{
id: 18,
parentId: 16,
},
{
id: 17,
parentId: 16,
},
],
},
],
};
代码实现:
function convert(list, parentKey, currentKey, rootValue) {
let result = {};
result.id = rootValue;
let children = list.filter((item, idx) => {
if (item[parentKey] === rootValue) list.splice(idx, 1);
return item[parentKey] === rootValue;
});
if (children.length > 0) {
result.children = children;
list.forEach((listItem, idx) => {
result.children.forEach((child, i) => {
if (listItem[parentKey] === child[currentKey]) {
if (!child.children) {
child.children = [];
}
child.children.push(listItem);
}
});
});
}
return result;
}