JS1-7

作者: Summerdise | 来源:发表于2019-12-09 23:51 被阅读0次
    • 按字面值创建数组
    var arr = [1,2,3,4,5];
    
    • 按构造方法创建数组
    var arr = new Array(1,2,3,4);
    //特殊的如果只写一个数字就是创建长度为该数字的一个数组
    var arr2 = new Array(5);//创建一个长度为5的数组arr2
    

    var a = '[a, b, c, d]';是一个string类型的变量
    var b = [1, 2, 3, 4];是一个数组类型的变量

            var a = [1, 2, 3, 4, 5];
            for (var i = 0; i < a.length; i++) {
                a[i] *= 2;
            }
            console.log(a);
    
            var colors = ["Red", "Green", "White", "Black"];
            var case1 = "";
            for (var i = 0; i < colors.length; i++) {
                case1 = case1 + colors[i] + " ";
            }
            case1 = case1.slice(0, case1.length - 1);
            case2 = case1.replace(/\s+/g, "+");
            case3 = case1.replace(/\s+/g, ",");
            console.log(case1);
            console.log(case2);
            console.log(case3);
    
            var a = [5, 1, 8, 10, 4];
    
            function compare(val1, val2) {
                if (val1 < val2) {
                    return -1;
                } else if (val1 > val2) {
                    return 1;
                } else {
                    return 0;
                }
            }
            a = a.sort(compare);
            a = a.reverse();
            console.log(a);
    
            var a = [3, 'a', 'a', 'a', 2, 3, 'a', 3, 'a', 2, 4, 9, 3];
            var elements = [a[0]];
            var times = [1];
            for (var i = 1; i < a.length; i++) {
                var ele = a[i];
                for (var j = 0; j < elements.length; j++) {
                    if (j == elements.length - 1 && ele !== elements[j]) {
                        elements[elements.length] = ele;
                        times[times.length] = 1;
                    } else if (ele === elements[j]) {
                        times[j]++;
                        break;
                    }
                }
            }
            var maxNum = 0;
            var maxIndex = 0;
            for (var i = 0; i < times.length; i++) {
                if (times[i] > maxNum) {
                    maxNum = times[i];
                    maxIndex = i;
                }
            }
            console.log(elements[maxIndex]);
    

    相关文章

      网友评论

          本文标题:JS1-7

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