<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ### 1、假設你有一個函數,產生[0, 5)之間的隨機整數,每個數字概率1/5,如何使用這個函數產生[0, 7)之間的隨機整數,每個數字概率1/7 ``` javascript /** 利用rand5()函數生成1-25之間的數字,然后將其中的1-21映射成1-7,丟棄22-25。 **/ function rand7() { var res = 0 do { res = (rand5() - 1) * 5 + rand5() } while(res > 21) return 1 + res % 7 } ``` ### 2、完成方法shuffle(arr),傳入數組arr,返回隨機打亂后的數組 ``` javascript function shuffle(arr) { return arr.sort(function() { return Math.random() - 0.5 }) } function shuffle(arr) { for(let i = arr.length; i; i--) { let j = ~~(Math.random() * i); [arr[i - 1], arr[j]] = [arr[j], arr[i - 1]]; } return arr; } ``` ### 3、完成方法count(str),傳入字符串,返回字符串中出現次數最多的字符及次數 ``` javascript function count(str) { return [...new Set(str.split(''))] .map(v => [v, str.match(new RegExp(v, 'g')).length]) .sort((a, b) => b[1] - a[1]) .slice(0, 1) } ``` ### 4、完成方法subset(arr1,arr2),傳入兩個數組,判斷數組arr1是否為數組arr2的子集 ``` javascript function subset(arr1, arr2) { if ((!arr1 instanceof Array) || (!arr2 instanceof Array)) return false if (arr2.length < arr1.length) return false for (let i = 0, len = arr1.length; i < len; i++) { if (arr2.indexOf(arr1[i]) === -1) return false } return true } ``` ### 5 、有一個數組: ``` javascript const arr = [[1,2],3,[4,5,6]]; ``` 定義一個函數,傳入arr后,返回值為一個二維數組: ``` javascript [[1,3,4],[2,3,4],[1,3,5],[2,3,5],[1,3,6],[2,3,6]] ``` ``` javascript function multiply(arr) { let ret = [] function cur(result, index) { if (index === -1) { ret.push(result) } else { let items = Array.isArray(arr[index]) ? arr[index] : [arr[index]] items.forEach(item => { cur([item, ...result], index - 1) }) } } cur([], arr.length - 1) return ret } const arr = [ [1, 2], 3, [4, 5, 6] ]; console.log(multiply(arr)) ``` ### 6、前端路由簡單實現 ``` javascript function Router() { this.routes = {} this.currentUrl = '' } Router.prototype.route = function(path, callback) { this.routes[path] = callback || function() {} } Router.prototype.refresh = function() { this.currentUrl = location.hash.slice(1) || '/' this.routes[this.currentUrl]() } Router.prototype.init = function() { window.addEventListener('load', this.refresh.bind(this), false) window.addEventListener('hashchange', this.refresh.bind(this), false) } window.Router = new Router() window.Router.init() Router.route('/', function() { console.log('white') }) Router.route('/blue', function() { console.log('blue') }) Router.route('/green', function() { console.log('green') }) ``` ### 7、有一個已經排序的數組,比方[1,4,6,9,11,15,18],給你一個新的數,插入到數組中,寫一個function ``` javascript var arr = [1, 4, 6, 9, 11, 15, 18] function arrIndexOf(arr, val) { var mid, min = 0, max = arr.length - 1 while (min <= max) { mid = (min + max) >> 1 if (val > arr[mid]) { min = mid + 1 } else if (val < arr[mid]) { max = mid - 1 } else { return mid } } return min } function insert(arr, val) { if (arr[0] === val) { arr.unshift(val) } else if (arr[arr.length - 1] === val) { arr.push(val) } else { arr.splice(arrIndexOf(arr, val), 0, val) } return arr } console.log(insert(arr, 17)) ``` ``` javascript var arr = [1, 4, 6, 9, 11, 15, 18] function insert(arr, val) { arr.push(val) return arr.sort((a, b) => { return a - b }) } console.log(insert(arr, 17)) ``` ### 8、 ``` bash 實現一個 HardMan: HardMan("jack") 輸出: I am jack HardMan("jack").rest(10).learn("computer") 輸出 I am jack //等待10秒 Start learning after 10 seconds Learning computer HardMan("jack").restFirst(5).learn("chinese") 輸出 //等待5秒 Start learning after 5 seconds I am jack Learning chinese ``` ``` javascript const HardMan = name => { class HardMan { constructor(name) { this.queue = [this.init(name)] setTimeout(async () => { for (let todo of this.queue) { await todo() } }, 0) } init(name) { return () => console.log(`I am ${name}`) } learn(subject) { this.queue.push(() => console.log(`Learning ${subject}`)) // 鏈式調用 return this } holdon(time) { return () => new Promise(resolve => setTimeout(() => { resolve(console.log(`Start learning after ${time} second`)) }, time * 1000)) } rest(time) { this.queue.push(this.holdon(time)) return this } restFirst(time) { this.queue.unshift(this.holdon(time)) return this } } return new HardMan(name) } HardMan("jack") HardMan("jack").rest(10).learn("computer") HardMan("jack").restFirst(5).learn("chinese") ``` 9、某團隊一共有 n 名成員,決定出去秋游,在海邊遇到出租房子的邦德羅,羅先生手上有 m 套待出租的房子,價格分別是 b1 、b2 ... bm; 由于習慣了微信支付,團隊中每個人身上的現金都有限,分別是 a1 a2 ... an,對了,一起出門的老板還帶有 S 元的團隊經費,這個經費是每個人都可以使用的 那么考慮以下兩個場景 場景1 團隊成員都很有愛,都愿意借錢給其他同事,那么這時候團隊最多能租到多少房子 ``` javascript function max(Array n, Array m, S) { return num } ``` 場景2 團隊成員都十分小氣,是不愿意借錢給別人的 ``` javascript //請判斷團隊成員是否都能租到房子 function isAll(Array n, Array m, S){ return bool } ``` > 做法待商榷 場景1: ``` javascript function max(Array n, Array m, S) { let count = 0 n.forEach(item => { count += item }) count += S let num = 0 let mSort = m.slice().sort((a, b) => { return a - b }) mSort.forEach(item => { count -= item if (count >= 0) num++ }) return num } ``` 場景2: ``` javascript function isAll(n, m, S) { let bool = null; let arr = new Array(n.length); const avg = S/n; n.forEach((item, index) => { arr[index] = item + avg }); let mSort = m.slice().sort((a, b) => { return a - b }); let arrSort = arr.slice().sort((a, b) => { return a - b }); for(let i = 0, len = arrSort.length; i < len; i++) { if (arrSort[i] < mSort[i]) { bool = false; break; } bool = true; } return bool; } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看