练习归纳1

47 阅读2分钟
  1. 值类型 console.log('packt' == true); // 结果为false toNumber(true) = 1 ; toNumber(packt) = 'NaN; Nan == 0; false

  2. ES6

    • let const 声明变量

    • 模板字面量

    • 解构

      • 一次初始化多个变量 let [x, y] = ['a', 'b'];
      • 互换值 [x, y] = [y, x];
    • 展开运算符 ...

    • 箭头函数=>

    • 模块

  3. Typescript

    • npm ls typescript -g // 查询是否安装全局typescript

    • npm install -g typescript // 全局安装

    • tsc -v // 查看安装版本

  4. 数组

    • 声明 初始化数组
        // 斐波那契数列
        const fibonacci = []
        fibonacci[1] = 1
        fibonacci[2] = 1
        for (let i = 3; i < 20; i++) {
            fibonacci[i] = fibonacci[i-1] + fibonacci[i-2]   
            for (let j = 0; j < fibonacci.length; j++) {
    
                console.log(fibonacci[j])
            }
        }
    
    • 添加

      • push
        •   numbers[numbers.length] = 10;
          
      • shift // Array原型添加方法,新增数据同时将其它数据往后移一位
        •   Array.prototype.insertFirstPosition = function(value) {
                for(let i = this.length;i>0;i--){
                    this[i] = this[i-1]
                }
                this[0] = value
            }
                arr.insertFirstPosition(-1)
          
      array.png
      • pop 删除元素
        •   for(let i = 0;i< arr.length;i++){
                arr[i] = arr[i+1]
            }
          
      arr1.png
        左移一位,最后一个数据为undefined,没有删除掉,用新数组push每个项,if(undefined){return}
        
        
      
    • 数组方法

      • concat
      • filter
      • forEach
      • join
      • indexOf
      • lastIndexOf
      • map
      • reverse
      • slice
      • some
      • sort
      • toString
      • valueOf
      • includes
      • find
      • findIndex
    • 迭代器 iterator

      • entries arr.entries() // 键值对
      • keys arr.keys() // 键
      • values arr.values() // 值
    • 创建新数组

      • Array.from(arr,x=>(x%2==0)) // 创建值为偶数的新数组
      • arr.fill(0) // 参数1 填充数据 参数2 起始位置 参数3 结束位置
        • Array(6).fill(1)
    • 类型数组

      • 类型数组则用于存储单一类型的数据

      •   // let myArray = new TypedArray (length)
          let int16 = new Int16Array(length);
          
        
  5. 栈数据结构

    • 栈是一种遵从后进先出(LIFO)原则的有序集合
    • class Stack {constructor() {this.items = []}}
      • push()
      • pop()
      • peek() 返回栈顶元素,不改变原栈
      • isEmpty()
      • clear()
      • size()
    // O(1)
    class Stack {
        constructor() {
            this.items = {}
            // 用一个count属性来帮助我们记录栈的大小(也能帮助我们从数据结构中添加和删除元素
            this.count = 0
        }
        push(element) {
            this.items[this.count] = element
            this.count++
        }
        pop() {
            if (this.isEmpty()) {
                return undefined
            }
            this.count--
            const result = this.items[this.count]
            delete this.items[this.count]
            return result
        }
        isEmpty() {
            return this.count === 0
        }
        clear() {
            this.items = {}
            this.count = 0
        }
        peek() {
            if (this.isEmpty()) {
                return undefined
            }
            return this.items[this.count - 1]
        }
    }
    
    // 10进制转换其它算法
    function baseConverter(decNumber, base) {
        const remStack = new Stack();
        const digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; // {6}
        let number = decNumber;
        let rem;
        let baseString = '';
    
        if (!(base >= 2 && base <= 36)) {
          return '';
        }
    
        while (number > 0) {
          rem = Math.floor(number % base);
          remStack.push(rem);
          number = Math.floor(number / base);
        }
    
        while (!remStack.isEmpty()) {
          baseString += digits[remStack.pop()]; // {7}
        }
    
        return baseString;
      }
    
  6. 队列和双端队列

    • enqueue(element) // 队尾添加
    • dequeue() // 移除队列第一项
    • peek()
    • isEmpty()
    • size()

    • addFront

    •   // 双端队列
        class Deque {
            constructor() {
                this.count = 0
                this.lowestCount = 0
                this.items = {}
            }
            addFront(element) {
                if (this.isEmpty()) {
                    this.addBack()
      
                } else if (this.lowestCount > 0) {
                    this[this.lowestCount] = element
      
                } else {
                    for (let i = this.count; i > 0; i--) {
                        this.items[i] = this.items[i - 1]
                    }
                    this.count++
                    this.lowestCount = 0
                    this.items[0] = element
                }
            }
            isEmpty() {
                return this.size() === 0
            }
            size() {
                return this.count - this.lowestCount;
            }
            addBack(element) {
      
                this.items[this.count] = element
                this.count++
            }
            removeFront() {
      
                if (this.isEmpty()) {
                    return undefined;
                }
                const rf = this.items[this.lowestCount]
                delete this.items[this.lowestCount]
                this.lowestCount++
                return rf
            }
            removeBack() {
                if (this.isEmpty()) {
                    return undefined;
                }
                const rb = this.items[this.count - 1]
                delete this.items[this.count]
                this.count--
                return rb
            }
            peekFront() {
                if (this.isEmpty()) {
                    return undefined
                }
                return this.items[this.lowestCount]
            }
            peekBack() {
                if (this.isEmpty()) {
                    return undefined
                }
                return this.items[this.count - 1]
            }
            clear() {
                this.count = 0
                this.lowestCount = 0
                this.items = {}
            }
        }