摘要:以下遞歸函數(shù)存在棧溢出的風(fēng)險(xiǎn),請(qǐng)問(wèn)如何優(yōu)化解答請(qǐng)實(shí)現(xiàn)一個(gè)計(jì)算最大公約數(shù)的函數(shù)在這里編寫代碼解答數(shù)組去重如果數(shù)組中有排除用實(shí)現(xiàn)斐波那契數(shù)列函數(shù)返回第個(gè)斐波那契數(shù)。父類我的名字是我今年歲原型繼承構(gòu)造函數(shù)繼承組合繼承組合繼承優(yōu)化
1.以下遞歸函數(shù)存在棧溢出的風(fēng)險(xiǎn),請(qǐng)問(wèn)如何優(yōu)化?
function factorial(n){ return n*factorial(n-1) }
解答:
function factorial(n){ return n > 1 ? n * factorial(n-1) : 1; }2.請(qǐng)實(shí)現(xiàn)一個(gè)計(jì)算最大公約數(shù)的函數(shù):
function greatestCommonDivisor(a,b){ //在這里編寫代碼 } greatestCommonDivisor(8, 12) //4 greatestCommonDivisor(8, 16) //8 greatestCommonDivisor(8, 17) //1
解答:
function greatestCommonDivisor(a,b){ var num=0; while(b!=0){ num=a%b; a=b; b=num; } return a; }3.數(shù)組去重(如果數(shù)組中有NaN)
Array.prototype.uniq = function () { var resArr = []; var flag = true; for(var i=0;i4.用 JavaScript 實(shí)現(xiàn)斐波那契數(shù)列函數(shù),返回第n個(gè)斐波那契數(shù)。 f(1) = 1, f(2) = 1 等 function fibonacci(n) { if(n ==1 || n == 2){ return 1 } return fibonacci(n - 1) + fibonacci(n - 2); }5.根據(jù)包名,在指定空間中創(chuàng)建對(duì)象輸入描述:
namespace({a: {test: 1, b: 2}}, "a.b.c.d")輸出描述:
{a: {test: 1, b: {c: {d: {}}}}}function namespace(oNamespace, sPackage) { var properties = sPackage.split("."); var parent = oNamespace; for (var i = 0, lng = properties.length; i < lng; ++i) { var property = properties[i]; if (Object.prototype.toString.call(parent[property])!== "[object Object]") { parent[property] = {}; } parent = parent[property]; } return oNamespace; }6.封裝函數(shù) f,使 f 的 this 指向指定的對(duì)象function bindThis(f, oTarget) { return function(){ var parames = Array.prototype.slice.call(arguments); return f.apply(oTarget,parames); //注意這里需要返回f的執(zhí)行結(jié)果 } }7.dom 節(jié)點(diǎn)查找查找兩個(gè)節(jié)點(diǎn)的最近的一個(gè)共同父節(jié)點(diǎn),可以包括節(jié)點(diǎn)自身
輸入描述:
oNode1 和 oNode2 在同一文檔中,且不會(huì)為相同的節(jié)點(diǎn)function commonParentNode(oNode1, oNode2) { if(oNode1.contains(oNode2)){ return oNode1; }else if(oNode2.contains(oNode1)){ return oNode2; }else{ return commonParentNode(oNode1.parentNode,oNode2); } }8.關(guān)系型數(shù)組轉(zhuǎn)換成樹形結(jié)構(gòu)對(duì)象關(guān)系型數(shù)組:
var obj = [ { id:3, parent:2 }, { id:1, parent:null }, { id:2, parent:1 }, ]期望結(jié)果:
o = { obj: { id: 1, parent: null, child: { id: 2, parent: 1, child: { id: ,3, parent: 2 } } } }實(shí)現(xiàn)源碼:
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] }或者:
function treeObj(obj) { return obj.sort((a, b) => b.parent - a.parent) .reduce((acc, cur) => (acc ? { ...cur, child: acc } : cur)); }9.JS如何判斷一組數(shù)字是否連續(xù)// 當(dāng)出現(xiàn)連續(xù)數(shù)字的時(shí)候以‘-’輸出 [1, 2, 3, 4, 6, 8, 9, 10]期望結(jié)果:
["1-4", 6, "8-10"]實(shí)現(xiàn)代碼:
判斷是否連續(xù):
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; };格式化實(shí)現(xiàn):
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; }10.創(chuàng)建子類Child,使用原型和構(gòu)造函數(shù)的方式繼承父類People的方法,并調(diào)用say函數(shù)說(shuō)出姓名和年齡。父類:
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()構(gòu)造函數(shù)繼承:
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()組合繼承優(yōu)化:
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()
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/101830.html
摘要:協(xié)商緩存從緩存數(shù)據(jù)庫(kù)中取出緩存的標(biāo)識(shí),然后向?yàn)g覽器發(fā)送請(qǐng)求驗(yàn)證請(qǐng)求的數(shù)據(jù)是否已經(jīng)更新,如果已更新則返回新的數(shù)據(jù),若未更新則使用緩存數(shù)據(jù)庫(kù)中的緩存數(shù)據(jù)。 1.CSS的盒子模型 包含元素內(nèi)容content、內(nèi)邊距padding、邊框border、外邊距marginbox-sizing:border-box;content-box;inherit;1) content-box:總寬度=mar...
摘要:但這會(huì)帶來(lái)高度塌陷的問(wèn)題,所以要清除浮動(dòng)。核心內(nèi)置類,會(huì)嘗試先于例外的是,利用的是轉(zhuǎn)換。安全性請(qǐng)求可被緩存,請(qǐng)求保存在瀏覽器的歷史記錄中則不能被緩存。與相比,的安全性較差,因?yàn)榘l(fā)送的數(shù)據(jù)是的一部分。 1.實(shí)現(xiàn)三欄布局(左右兩邊固定寬度,中間自適應(yīng)) 1)浮動(dòng)布局左右兩邊固定寬度,并分別設(shè)置float:left和float:right。(但這會(huì)帶來(lái)高度塌陷的問(wèn)題,所以要清除浮動(dòng)。清除浮動(dòng)...
摘要:前段時(shí)間,前同事跳槽,機(jī)緣巧合下面了阿里,本來(lái)憑著試一試的態(tài)度,卻不料好事成雙,拿到了,而且薪資也了。面就沒(méi)啥東西可聊的,基本上就是對(duì)此次面試的一個(gè)評(píng)價(jià)定薪等等一些之內(nèi)的話題。如果是現(xiàn)場(chǎng)面試,記得關(guān)注當(dāng)天的天氣,提前查一下路線。 ...
摘要:我的是忙碌的一年,從年初備戰(zhàn)實(shí)習(xí)春招,年三十都在死磕源碼,三月份經(jīng)歷了阿里五次面試,四月順利收到實(shí)習(xí)。因?yàn)槲倚睦砗芮宄?,我的目?biāo)是阿里。所以在收到阿里之后的那晚,我重新規(guī)劃了接下來(lái)的學(xué)習(xí)計(jì)劃,將我的短期目標(biāo)更新成拿下阿里轉(zhuǎn)正。 我的2017是忙碌的一年,從年初備戰(zhàn)實(shí)習(xí)春招,年三十都在死磕JDK源碼,三月份經(jīng)歷了阿里五次面試,四月順利收到實(shí)習(xí)offer。然后五月懷著忐忑的心情開始了螞蟻金...
摘要:拿到秋招的同學(xué),如確定入職需與用人單位簽署三方協(xié)議,以保證雙方的利益不受損失。當(dāng)然每個(gè)崗位所要求的側(cè)重點(diǎn)不同,但卻百變不離其宗。方法論要想達(dá)成某個(gè)目標(biāo)都有其特定的方法論,學(xué)習(xí)技術(shù)也不例外,掌握適當(dāng)?shù)膶W(xué)習(xí)方法才能事半功倍。 寫在前面的話 筆者從17年的2月份開始準(zhǔn)備春招,其中遇到不少坑,也意識(shí)到自己走過(guò)的彎路。故寫了這篇文章總結(jié)一番,本文適合主動(dòng)學(xué)習(xí)的,對(duì)自己要學(xué)的課程不明確的,對(duì)面試有...
閱讀 2391·2021-11-22 14:56
閱讀 10872·2021-09-08 10:45
閱讀 2071·2019-08-30 13:54
閱讀 2921·2019-08-29 16:54
閱讀 2089·2019-08-29 14:20
閱讀 1845·2019-08-29 12:25
閱讀 1909·2019-08-29 12:17
閱讀 1108·2019-08-23 18:29