对于具有使用数值的多个变量的循环数组

时间:2014-09-28 15:23:45

标签: javascript

我正在尝试在数组中使用for循环,以便在html中点击按钮加上时间动作时按排序顺序显示随机数(从高到低),我已经完成了这个,它的工作原理是在100到1的新行上显示每个数字的结果,并以毫秒为单位给出时间。

我要做的是让它每行显示10个数字,但不知道如何去做。

的Javascript

function Numberordermatch() {

    var start = new Date().getTime();

    for (i = 0; i < 1000; ++i) {

    var points = [77, 57, 18, 35, 36, 33, 43, 87, 100, 14, 73, 97, 96, 27, 34, 39, 23, 71, 1, 86, 56, 21, 26, 65, 20, 29, 55, 49, 16, 42, 90, 91, 59, 84, 38, 75, 82, 66, 17, 62, 30, 63, 74, 89, 22, 50, 68, 31, 78, 81, 44, 93, 9, 40, 41, 48, 19, 32, 46, 28, 53, 70, 52, 60, 80, 47, 15, 069, 7, 67, 13, 61, 5, 94, 6, 98, 99, 83, 76, 88, 25, 72, 79, 37, 51, 64, 8, 2, 92, 12, 54, 045, 3, 58, 11, 95, 24, 10, 85, 4];

    points.sort(function(a, b){return b-a});
    document.getElementById("demo").innerHTML = points;

}

    var end = new Date().getTime();
    var time = end - start;

    text = "";
    var i;
    for (i = 0; i < points.length; i++) {
        text += points[i] + "<br>";
    }

    document.getElementById("demo").innerHTML = text;

    document.getElementById("results").innerHTML =
    ('<strong>This function took: ' + time + 'ms to load</strong>');

}

1 个答案:

答案 0 :(得分:2)

根据Ian Brindley答案,可能是:

var text = '';
var points = [77, 57, 18, 35, 36, 33, 43, 87, 100, ...]
points.sort(function(a, b){return b-a});
for (i = 0; i < points.length; ++i) {
    text += points[i] + ", " + ((i % 10 === 0) ? '<br />' : '');
}
document.getElementById("demo").innerHTML = text;