Github – program-learning-lists
“开源(open source
)”这个词,指的是事物规划为可以公然接见的,因而人们可以修正并分享。
作为一个从大型同性交际网站GitHub / Rain120上进修生长的一个前端菜鸟的我,深受大佬的感染,也愿望偶然机可以给同样是菜鸟的你们供应协助,在我进修之余,留下了之前进修碰到的、网上看到的、笔试口试碰到的一些编程的解法,还请列位大佬手下留情,多多提拔、指导我等菜鸟。
迎接列位大佬留下越发简朴的写法,感谢!!
代码堆栈: Web-Study
空话不多说,先上题吧。
1、关联型数组转换成树形组织对象(拼多多2018前端笔试真题 / 依图口试)
关联型数组
var obj = [
{ id:3, parent:2 },
{ id:1, parent:null },
{ id:2, parent:1 },
]
希冀结果:
o = {
obj: {
id: 1,
parent: null,
child: {
id: 2,
parent: 1,
child: {
id: ,3,
parent: 2
}
}
}
}
思绪:
先找到它的根元素,依据id
和parent
来推断它们之间的关联
完成源码:
function treeObj(obj) {
obj.map(item => {
if (item.parent !== null) {
obj.map(o => {
if (item.parent === o.id) {
if (!o.child) {
o.child = [];
}
o.child.push(item);
o.child = o.child;
}
});
}
});
return obj.filter(item => item.parent === null)[0]
}
解法2:(知乎批评区供应的要领)
function treeObj(obj) {
return obj.sort((a, b) => b.parent - a.parent)
.reduce((acc, cur) => (acc ? { ...cur, child: acc } : cur));
}
完成结果:
2、请用 javascript 完成一个函数 parseUrl(url),将一段 url字符串剖析为 Object。(西洋汇笔试)
parseUrl("http://www.xiyanghui.com/product/list?id=123456&sort=discount#title");
希冀结果:
{
protocol: "http",
host: "www.xiyanghui.com",
path: "/product/list",
params: {
id: "12345",
sort: "discount"
},
hash: "title"
}
思绪:
起首我们须要相识一下URL
这个观点。
URL(一致资本定位符)(或称一致资本定位器/定位地点、URL
地点等,英语:Uniform Resource Locator
,常缩写为URL
),偶然也被俗称为网页地点(网址)。
这里,我补充一点,列位不仅须要相识 URL
,还须要相识 URI
。
URI:一致资本标识符(英语:Uniform Resource Identifier
,或URI
)是一个用于标识某一互联网资本称号的字符串。 该种标识许可用户对收集中(平常指万维网)的资本经由过程特定的协定举行交互操纵。URI
的最常见的情势是一致资本定位符(URL
),常常指定为非正式的网址。更稀有的用法是一致资本称号(URN
),其目标是经由过程供应一种门路。用于在特定的名字空间资本的标识,以补充网址。
下图展现了两个 URI 例子及它们的组成部分。
URL
平常包含协定
、域名
、端口
、query
、params
等,我们在传入参数时是字符串,我们须要将它转换成URL
,可以经由过程建立一个a
标签来将字符串 转换成URL
。
完成源码:
function parseUrl(url) {
var a = document.createElement('a');
a.href = url;
return {
source: url,
protocol: a.protocol.replace(':',''),
host: a.hostname,
port: a.port,
query: a.search,
params: (() => {
var ret = {}, querys = [];
var searchQuery = a.search.replace(/^\?/,'').split('&');
for ( var i = 0;i < searchQuery.length; i++) {
if (searchQuery[i]) {
querys = searchQuery[i].split('=');
ret[querys[0]] = querys[1];
}
}
return ret;
})(),
file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
hash: a.hash.replace('#',''),
path: a.pathname.replace(/^([^\/])/,'/$1'),
relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],
segments: a.pathname.replace(/^\//,'').split('/')
};
}
parseUrl("http://www.xiyanghui.com/product/list?id=123456&sort=discount#title");
完成结果:
3、数组元素的分列组合题目
[1, 2, 3]
希冀结果:
全分列:
[[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]]
思绪:
每次取数组中的一个数,然后将盈余的数,顺次到场到该数的组合数组中
完成代码:
function Permutations (target, size, origin) {
var _arr = []
function getArrange(target, nums, ret) {
if (nums === 1) {
for (var i = 0; i < target.length; i++) {
var tmp = ret.slice();
tmp.push(target[i]);
_arr.push(tmp);
}
} else {
nums -= 1;
for (var i = 0; i < target.length; i++) {
var tmp = ret.slice();
var newTarget = target.slice();
tmp.push(target[i]);
newTarget.splice(i, 1);
getArrange(newTarget, nums, tmp);
}
}
}
getArrange(target, size, origin);
return _arr;
}
Permutations([1, 2, 3], 2, [])
完成结果:
[1, 2, 3, 4]
分列组合:
[[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]
思绪:
取一个数,到场到数组中,接着再取一个数到场数组,直到须要的num为0。
完成代码:
function Permutations (arr, nums) {
var ret = [];
function getRet(origin, target, nums) {
if (nums === 0) {
ret[ret.length] = origin;
return;
}
for (var i = 0; i <= target.length - nums; i++) {
var tmp = origin.slice();
tmp.push(target[i]);
getRet(tmp, target.slice(i + 1), nums - 1);
}
}
getRet([], arr, nums);
return ret;
}
Permutations([1, 2, 3, 4], 3)
完成结果:
4、JS怎样推断一组数字是不是一连(百词斩笔试题)
// 当涌现一连数字的时刻以‘-’输出
[1, 2, 3, 4, 6, 8, 9, 10]
希冀结果:
["1-4", 6, "8-10"]
道理:
推断前后数字是不是相差为1,如果是则到场同一个数组
完成代码:
推断是不是一连:
var arrange = function(arr){
var result = [],temp = [];
arr.sort(function(source, dest){
return source - dest;
}).concat(Infinity).reduce(function(source, dest){
temp.push(source);
if(dest-source > 1){
result.push(temp);
temp = [];
}
return dest;
});
return result;
};
完成结果:
格式化完成:
var formatarr = function(arr) {
var newArr = []
var arr1 = arrange(arr)
for (var i in arr1) {
var str = '';
if (arr1[i].length > 1) {
str = arr1[i][0] + '-' + arr1[i][arr1[i].length - 1];
newArr.push(str)
} else {
newArr.push(arr1[i][0]);
}
}
return newArr;
}
完成结果:
5、建立子类Child,运用原型和组织函数的体式格局继续父类People的要领,并挪用say函数说出姓名和岁数。(详见我的另一篇文章前端口试题目之面向对象类)
完成代码:
父类:
function People(name,age){
this.name=name;
this.age=age;
this.say=function(){
console.log("我的名字是:"+this.name+"我本年"+this.age+"岁!");
};
}
原型继续:(原型链相干题目请看我的另一篇文章前端口试之原型链题目)
function Child(name, age){
this.name = name;
this.age = age;
}
Child.prototype = new People();
var child = new Child('Rainy', 20);
child.say()
完成结果:
组织函数继续
function Child(name, age){
People.call(this)
this.name = name;
this.age = age;
}
var child = new Child('Rainy', 20);
child.say()
完成结果:
组合继续:
function Child(name, age){
People.call(this);
this.name = name;
this.age = age;
}
Child.prototype = People.prototype;
var child = new Child('Rainy', 20);
child.say()
完成结果:
组合继续优化:
function Child(name, age){
People.call(this);
this.name = name;
this.age = age;
}
Child.prototype = Object.create(People.prototype);
Child.prototype.constructor = Child;
var child = new Child('Rainy', 20);
child.say()
完成结果:
未完待续