Brush some test questions recently, consolidate the foundation, make a note, as the progress of doing questions, will continue to update, welcome correction

1, input a positive integer, output all its prime factors in descending order (repeat the list) (for example, the prime factors of 180 are 2, 2, 3, 3, 5). The last number should also be followed by a space

function getPreGens(a) { var arr = []; var n = Math.floor(a/2)+ 1; // var t = n; // var i; while(true) { if(i==t) { arr.push(a); break; } for(I =2; i<t; i++) { if(a%i == 0) { arr.push(i); a = a/i; t = Math.floor(a/2 )+ 1; break; // Stop for loop}}} return arr; } console.log(getPreGens(180)); / / 2,2,3,3,5Copy the code

2. Bubble sort

Const aa = [8,94,15,88,55,76,21,39] function getSort(param = []) {let num = null for (let I = 0; i < param.length - 1; i++) { for (let j = 0; j < param.length - 1 - i; j++) { if (param[j] > param[j + 1]) { num = param[j] param[j] = param[j + 1] param[j + 1] = num } } } return param } console.log(getSort(aa))Copy the code
  • Principle two cycles
  1. When I =0, the loop executes completely, from j=0 to j=6. This is the first sorting, and the result is that the largest number is ranked last. At the end of the loop, the result should be [8,15,88,55,76,21,39,94].
  2. When I =1, the loop is completed again. Since the largest number is already at the end, there is no need to compare the last two items of the array. This is also the nifty part of j
  3. At this point, the rule is clear, we’re going to put the largest number left in the array at the end, and when we get to the end of the first loop, which is I =6, at which point, j=0, we just compare the first and second items in the array, and when we’re done, we return.

3, fast row

const aa = [1, 2, 44, 11, 232, 23] Const pivotSort = function (arr) {if (arr.length <= 1) {return arr // end} const pivotIndex = math.floor (arr.length) / 2) const pivot = pivot. Splice (pivotIndex, 1)[0] let left = [] let right = [] for (var I = 0; i < arr.length; i++) { if (arr[i] < pivot) { left.push(arr[i]) } else { right.push(arr[i]) } } return quickSort(left).concat([pivot], QuickSort (right))// Recursion} console.log(quickSort(aa))Copy the code

4. Implement a deep copy

Function copyObj(obj) {let cloneObj if (obj && typeof obj! Else if (obj && typeof obj === 'object') {// Check whether the input data is an array or an object cloneObj = Array.isArray(obj) ? [] : {} for (let key in obj) {if (obj. HasOwnProperty (key)) {if (obj[key] && typeof obj[key] === 'object') {// For (let key in obj) {if (obj[key] && typeof obj[key] === 'object') CloneObj [key] = copyObj(obj[key])} else {cloneObj[key] = obj[key]}}}} return cloneObj}Copy the code

5. Simple publish/subscribe and observer models

  • Publish the subscriber model
class EventClass { _events = {} on = (key, callBack) => { if (! this._events[key]) { this._events[key] = [] } this._events[key].push(callBack) } emit = (key, ... args) => { this._events[key].forEach((callBack) => { callBack(... args) }) } off(key, callBack) { this._events[key] = this._events[key].map((item) => { return item ! == callBack }) } once(key, callback) { const once = (... args) => { callback(... args) this.off(key, once) } this.on(key, once) } }Copy the code
  • Observer model
This. Observers = []; // This. Observers = []; } // add add(observer) {this.observers.push(observer); } // delete remove(observer) {let idx = this.observers. FindIndex (item => item === observer); idx > -1 && this.observers.splice(idx, 1); } // notify notify() {for (let observer of this.observers) {observer.update(); }}} class constructor {constructor(name) {this.name = name; } update() {console.log(' the target notifies me of the update, I am: ${this.name} '); }} let subject = new subject (); Let obs1 = new Observer(' front-end developer '); Let obs2 = new Observer(' backend developer '); // Add observer subject.add(obs1) to the target; subject.add(obs2); // Notify the target to update subject.notify(); // Output: // The target notifies me of an update, I am a front-end developerCopy the code

6. Singleton mode

Class Single {oneSingle = '' static installSingle = () => {if (! this.oneSingle) { this.oneSingle = new Single() return this.oneSingle } return this.oneSingle } }Copy the code

Flat data structures

Const menuList = [{pid: -1, name: 'cart', id: 1, auth: 'cart',}, {pid: 1, name: 'cart', id: 4, auth: 'cart - list',}, {pid: 4, name: 'lottery tickets, id: 5, auth:' lottery '}, {pid: 4, name: 'goods', id: 6, auth: 'product', }, Let shuxing = {} getThree(menuList) function getThree(param = []) {param.map((item) => { shuxing[item.id] = item shuxing[item.id].children = [] if (item.pid === -1) { tree = item } if (shuxing[item.pid]) { Shuxing [item.pid].children.push(item)}} // function getThree2(param = []) {let root = param.filter((item) => item.pid === -1), children = param.filter((item) => item.pid ! == -1) setThree22(root, children) return root function setThree22(root, children) { root.forEach((item) => { children.forEach((sutItem, subIndex) => { if (item.id === sutItem.pid) { let childrenArr = JSON.parse(JSON.stringify(children)) childrenArr.splice(subIndex, 1) setThree22([sutItem], childrenArr) if (item.children) { item.children.push(sutItem) } else { item.children = [sutItem] } } }) }) } } // Function getThree3(param = []) {const _param = json.parse (json.stringify (param)) return _param.filter((item) => { const _arr = _param.filter((subItem) => { return item.id === subItem.pid }) _arr && _arr.length > 0 && (item.children = _arr) return item.pid === -1 }) }Copy the code

7. Eldest child Sequence problem (Chorus)

/ / cow https://www.nowcoder.com/practice/6d9d69e3898f45169a441632b325c7b4?tpId=37&&tqId=21247&rp=1&ru=/ta/huawei&qru=/ta/huawei /question-ranking const line = Number(readline()); let arr = readline() ; let newArr = arr.split(' ') let dp =[]; For (let I =0; i<newArr.length; i++){ dp[i]=1; } let res = 0; for(let i=1; i<newArr.length; i++){ for(let j=0; j<i; j++){ if(i==7){ console.log('-dp-i00-',j,i,newArr[j],newArr[i]) } if(newArr[j]<newArr[i]){ dp[i] = Math. Max (dp [I], dp [j] + 1) / / dp [I] on behalf of the ith place rise subsequence the length of the elements in the if (I = = 7) {the console. The log (' - dp - I - 'newArr [I], dp, [I] I)}}} res = Math.max(res,dp[i]) } console.log(dp) console.log(res)Copy the code

8. Depth traversal and breadth traversal

const data = [ { name: 'a', children: [ { name: 'b', children: [{ name: 'e' }] }, { name: 'c', children: [{ name: 'f' }] }, { name: 'd', children: [{ name: 'g' }] }, ], }, { name: 'a2', children: [ { name: 'b2', children: [{ name: 'e2' }] }, { name: 'c2', children: [{ name: 'f2' }] }, { name: 'd2', children: [{ name: 'g2'}]},],},] // depth traversal, Function getName(data) {const result = [] data.foreach ((item) => {const map = (data) => {result.push(data.name) Data.children && data.children. ForEach ((child) => map(child))} map(item)}) return result.join(',')} Create an execution queue, Function getName2(data) {let result = [] let queue = data while (queue.length > 0) { [...queue].forEach((child) => { queue.shift() result.push(child.name) child.children && queue.push(... child.children) }) } return result.join(',') } console.log(getName(data)) console.log(getName2(data))Copy the code

9, handwriting instanceof implementation

// [1,2,3] instanceof Array ---- true // L instanceof R // function instance_of(L,R){// Verify that if it is a basic data type, Return false const baseType = ['string', 'number','boolean','undefined','symbol'] if(baseType.includes(typeof(L))) { return false } let RP = R.prototype; // take the display prototype of R L = l.__proto__; While (true){// For (;;); If (L === null){return false; } if(L === RP){return true; } L = L.__proto__; }}Copy the code