Summary of wrong questions in the sophomore exam

    <script>
        console.log("5" == 5); // 发生隐式类型转化     true  
        console.log(null == undefined); // true
        console.log(null === undefined); // false
        console.log(typeof(null)); // object  历史遗留问题
        console.log(typeof(undefined)); // undefined
        console.log((false == '')); // true
        console.log((false == 0)); // true
    </script>

Use js string method, etc.: Count
the number of occurrences of each character in a non-empty string (xuptxiyounet2020) and encapsulate this method into a function.

        const testStr = 'xuptxiyounet2020';

        function fn(str) {
    
    
            const obj = {
    
    };
            for (var i in str) {
    
    
                if (obj[str[i]]) {
    
    
                    obj[str[i]]++;
                } else {
    
    
                    obj[str[i]] = 1;
                }
            }
            return obj;
        }

        console.log(fn(testStr));

Question 8: Simple algorithmic question: Sort the following objects according to their age from youngest to oldest, and those with the same age
are sorted by name.
person1 = {name:'jane',age: 35}
person2 = {name:'maru',age: 23}
person3 = {name:'harvey',age: 21}
person4 = {name:'anne',age: twenty one}

    <script>
          var person1 = {
     
     
                name: 'jane',
                age: 35
            },
            person2 = {
     
     
                name: 'maru',
                age: 23
            },
            person3 = {
     
     
                name: 'harvey',
                age: 21
            },
            person4 = {
     
     
                name: 'anne',
                age: 23
            };

        var array1 = [person1, person2, person3, person4];
        array1.sort(function(a, b) {
     
     
            return a.age - b.age;
        });
        console.log(array1);
        array1.sort(function(a, b) {
     
     
            if (a.age == b.age) {
     
     
                if (a.name < b.name) {
     
     
                    return 1 // return 1 是升序
                } else {
     
     
                    return -1 // return -1 是降序
                }
            }
        });
        console.log(array1);

    </script>

Guess you like

Origin blog.csdn.net/weixin_45773503/article/details/108565341