zoukankan      html  css  js  c++  java
  • xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

    historyReverser & array reverse

    
    "use strict";
    
    /**
     *
     * @author xgqfrms
     * @license MIT
     * @copyright xgqfrms
     *
     * @description historyReverser
     * @augments Reverse 逆向 / Recursive 递归
     * @example
     * @link
     *
     */
    
    const historyReverser = (datas = [], text = ``, debug = false) => {
        let result = {};
        let newDatas = [];
        let reverseDatas = [];
        let length = datas.length;
        if (length === 10) {
            if(datas.includes(text)) {
                // remove old & add new
                let index = datas.indexOf(text);
                datas.splice(index, 1);
                datas.push(text);
            } else {
                // add new
                datas.push(text);
                datas.shift(0);
            }
        } else {
            datas.push(text);
        }
        newDatas = datas;
        datas.forEach(
            (item, i) => {
                let value = datas[length - i - 1];
                reverseDatas.push(value);
            }
        );
        result = {
            newDatas,
            reverseDatas,
        };
        return result;
    };
    
    
    export default historyReverser;
    
    export {
        historyReverser,
    };
    
    
    
    
    

    tesing

    
    let datas = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
    
    let text = "5";
    // let text = "15";
    const historyReverser = (datas = [], text = ``, debug = false) => {
        let result = {};
        let newDatas = [];
        let reverseDatas = [];
        let length = datas.length;
        if (length === 10) {
            if(datas.includes(text)) {
                // remove old & add new
                let index = datas.indexOf(text);
                datas.splice(index, 1);
                datas.push(text);
            } else {
                // add new
                datas.push(text);
                datas.shift(0);
            }
        } else {
            datas.push(text);
        }
        newDatas = datas;
        datas.forEach(
            (item, i) => {
                let value = datas[length - i - 1];
                reverseDatas.push(value);
            }
        );
        result = {
            newDatas,
            reverseDatas,
        };
        return result;
    };
    
    let ReverseArray = historyReverser(datas, text);
    
    console.log(`ReverseArray =`, ReverseArray);
    
    
    
    
    

    MDN & Array

    https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

    0.push

    1. shift

    2. unshift

    3. slice

    4. splice

    5. includes
      6.infdexOf
      7.last​IndexOf


    bug

    
    "use strict";
    
    /**
     *
     * @author xgqfrms
     * @license MIT
     * @copyright xgqfrms
     *
     * @description historyReverser
     * @augments Reverse 逆向 / Recursive 递归
     * @example
     * @link
     *
     */
    
    const historyReverser = (datas = [], text = ``, debug = false) => {
        if (text === null) {
            text = `xyz`;
        }
        let result = {};
        let newDatas = [];
        let reverseDatas = [];
        let length = datas.length;
        if (length === 10) {
            if(datas.includes(text)) {
                // remove old & add new
                let index = datas.indexOf(text);
                datas.splice(index, 1);
                datas.push(text);
            } else {
                // add new
                datas.push(text);
                datas.shift(0);
            }
            if(datas.includes(text)) {
                // remove old & add new
                let index = datas.indexOf(text);
                datas.splice(index, 1);
                datas.push(text);
            } else {
                // add new
                datas.push(text);
                datas.shift(0);
            }
        } else {
            datas.push(text);
        }
        newDatas = datas;
        // datas.forEach(
        //     (item, i) => {
        //         let value = datas[length - i - 1];
        //         console.log(`null value`, value);
        //         reverseDatas.push(value);
        //     }
        // );
        if (1 <= length) {
            datas.forEach(
                (item, i) => {
                    let value = datas[length - i - 1];
                    console.log(`null value`, value);
                    reverseDatas.push(value);
                }
            );
        } else {
            reverseDatas = datas;
        }
        result = {
            newDatas,
            reverseDatas,
        };
        return result;
    };
    
    
    export default historyReverser;
    
    export {
        historyReverser,
    };
    
    
    

    OK

    unshfit

    
    "use strict";
    
    /**
     *
     * @author xgqfrms
     * @license MIT
     * @copyright xgqfrms
     *
     * @description historyReverser
     * @augments Reverse 逆向 / Recursive 递归
     * @example
     * @link
     *
     */
    
    const historyReverser = (datas = [], text = ``, debug = false) => {
        // console.log(`datas =`, datas);
        // console.log(`text =`, text);
        let result = {};
        let newDatas = [];
        let reverseDatas = [];
        let length = datas.length;
        // if(datas.indexOf(text) !== -1) {
        if(datas.includes(text)) {
            // remove old & add new
            let index = datas.indexOf(text);
            datas.splice(index, 1);
            console.log(`datas =`, datas);
            datas.unshift(text);
            // datas.push(text);
        } else {
            // add new
            if (length === 10) {
                datas.splice(9, 1);
                datas.unshift(text);
                // datas.push(text);
                // datas.shift(0);
            } else {
                // add new
                datas.unshift(text);
                // datas.push(text);
            }
        }
        newDatas = datas;
        reverseDatas = datas;
        // let new_length = newDatas.length;
        // if (1 <= new_length) {
        //     let temp = datas;
        //     reverseDatas = temp.reverse();
        //     // datas.forEach(
        //     //     (item, i) => {
        //     //         let value = datas[new_length - i - 1];
        //     //         // console.log(`value`, value);
        //     //         reverseDatas.push(value);
        //     //     }
        //     // );
        // } else {
        //     reverseDatas = datas;
        // }
        result = {
            newDatas,
            reverseDatas,
        };
        return result;
    };
    
    
    export default historyReverser;
    
    export {
        historyReverser,
    };
    
    
    

  • 相关阅读:
    leetcode------Palindrome Number
    leetcode------Minimum Depth of Binary Tree
    leetcode------Binary Tree Level Order Traversal II
    leetcode------Plus One
    leetcode------Plus One
    leetcode------Min Stack
    leetcode------Binary Tree Level Order Traversal
    递归树与非递归树的不同实现【转载,个人感觉写的比较好的一篇,值得去思考】
    leetcode------Compare Version Numbers
    leetcode------Majority Element
  • 原文地址:https://www.cnblogs.com/xgqfrms/p/10881090.html
Copyright © 2011-2022 走看看