-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
3.31笔试题 #8
Comments
参考答案// 第1题
const getVal= (arr)=>{
const flat = (ary)=>{
return ary.reduce((pre,cur)=>{
if(cur === undefined) return [...pre];
return Array.isArray(cur)?[...pre,...flat(cur)]:[...pre,cur];
},[]);
}
return flat(arr);
}
console.log(getVal([1, 2, 3, [9, undefined, 8], 5, 6, 7]))
// 第2题
function shuffle(arr){
const ans = [];
while(arr.length>0){
const i = Math.floor(Math.random()*arr.length);
ans.push(arr.splice(i,1)[0]);
}
return ans;
}
const res = shuffle([1,2,3,4,5,6,7,8,9]);
console.log(res);
// 第3题
const nFloors=(n)=>{
if(n===1 || n===2) return n;
return nFloors(n-1)+nFloors(n-2);
}
console.log(nFloors(5));
//第4题
// 方式一:
JSON.parse(JSON.stringify(obj))
// 方式二:递归赋值
const deepClone = (obj)=>{
if(typeof obj !== 'object' || obj ===null) return obj;
const deepObj = Array.isArray(obj)?[]:{};
for(const item in obj){
if(typeof item === "object"){
deepObj[item] = deepClone(item);
}else{
deepObj[item] = item;
}
}
return deepObj;
}
const obj = {a:{b:{c:1}}}
const obj2 = deepClone(obj);
console.log(obj,obj2,obj===obj2);
// 第5题
const flattenTreeFn=(tree)=>{
const traverse = (tree,ans,str)=>{
str+=(str?'/':'')+tree.name;
if(!tree.children) {
ans.push({name:str});
str = '';
return
}
for(let item of tree.children){
traverse(item,ans,str);
}
}
const ans = [];
traverse(tree,ans,'');
console.log(ans);
return ans;
}
flattenTreeFn(treeSample);
// 第6题
const timeoutVal = (ms, val) => new Promise((r) => setTimeout(() => r(val), ms))
.then(e => {console.log(e); return e;});
const func1 = () =>timeoutVal(2000,1)
const func2 = () =>timeoutVal(1000,2)
const func3 = () =>timeoutVal(2000,3)
const executePromise= (ajaxArray) => {
const ans = [];
let count = 0;
const runTask = (task)=>{
return task().then(res=>{
ans[count] = res;
count++;
ajaxArray[count] && runTask(ajaxArray[count])
})
}
runTask(ajaxArray[0]);
return new Promise(resolve=>{
if(count === ajaxArray.length) resolve(ans);
});
};
executePromise([func1,func2,func3]).then(res=>{
console.log('done');
console.log(res);
});
// 第7题
const getUrlVal = (url)=>{
const arr = url.split('?').slice(1)[0].split('&');
const ans = {};
for(const item of arr){
const [key,value] = item.split('=');
ans[key] = value;
}
return ans;
}
const ans = getUrlVal('http://www.lubansoft.com/?a=1&b=2&c=&d=xxx&e')
console.log(ans);
// 第8题
//父div:display:flex;justify-content:center;align-items:center;
// 方式二
// 父div:position:relative;
// 子div:poisition:absolute;top:50%;left:50%;transform:translate(-50%,-50%); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
第1题
第2题
第3题
第4题
第5题
第6题
第7题
第8题
(上海鲁班软件)
The text was updated successfully, but these errors were encountered: