美文网首页
js题 - 运行结果

js题 - 运行结果

作者: 小王子__ | 来源:发表于2020-08-04 15:08 被阅读0次

    1,写出执行结果

    var num1 = 55
        var num2 = 66
        function f1 (num, num1) {
            num = 100
            num1 = 100
            num2 = 100
            console.log(num)
            console.log(num1)
            console.log(num2)
        }
        f1(num1, num2)
        console.log(num1)
        console.log(num2)
        console.log(num)
    
    image.png

    2,写出执行结果

    function Person (name, age, salary) {
            this.name = name
            this.age = age
            this.salary = salary
        }
        function f1 (person) {
            person.name = 'ls'
            person = new Person('aa', 18, 10)
        }
        var p = new Person('zs', 18, 1000)
        console.log(p.name)
        f1(p)
        console.log(p.name)
    
    function Person (name, age, salary) {
            this.name = name
            this.age = age
            this.salary = salary
        }
        function f1 (person) {
            person.name = 'ls'
            person = new Person('aa', 18, 10)
        }
        var p = new Person('zs', 18, 1000)
        console.log(p.name)
        f1(p)
        console.log(p.name)
    // 'zs'  'ls'
    

    3,已知字符串foo = 'get-element-by-id',写一个function将其转成驼峰表示法'getElementById'

        var foo = 'get-element-by-id'
        var arr = foo.split('-')   // split根据字符串进行切割 把字符串转成数组
        for (var i = 1; i < arr.length; i++) {
            arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].substr(1, arr[i].length)
        }
        console.log(arr.join(''))   // getElementById   join('') 将数组转成字符串
    
    对应方法:
        var foo = 'get-element-by-id'
        function toString(v) {
            var arr = v.split('-')
            for (var i = 1; i < arr.length; i++) {
                arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].substr(1, arr[i].length)
            }
            console.log(arr.join(''))   // getElementById
            return arr
        }
        toString(foo)
    

    4,反转数组 reverse

    var arr = [1, 2, 3, 4, 5, 6, 7, 8]
        function reverse (v) {
            for (var i = 0; i < v.length / 2; i++) {
                var temp = arr[i]
                v[i] = arr[v.length - 1 - i]
                v[v.length - 1 - i] = temp
            }
            console.log(v)
            return v
        }
        reverse(arr)    // [8, 7, 6, 5, 4, 3, 2, 1]
    

    5,去重
    1,简单的去重

    var arr = [8, 11, 20, 5, 20, 8, 0, 2, 4, 0, 8]
        function uniq (v) {
            var ary = []
            for (var i = 0; i < v.length; i++) {
                if (ary.indexOf(v[i]) === -1) {
                    ary.push(v[i])
                }
            }
            console.log(ary)
            return ary
        }
        uniq(arr)
    

    相关文章

      网友评论

          本文标题:js题 - 运行结果

          本文链接:https://www.haomeiwen.com/subject/oghwrktx.html