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