magi4000
magi4000

Reputation: 409

For loop array with multiple variables using numeric value

I'm trying to use a for loop in an array, to make it display the random numbers in sort order (High to lower) when a button is clicked on within the html plus time the action, I have done this and it works by displaying the results with each number on a new line from 100 to 1 and giving the time in milliseconds.

What I'm trying to do, is make it display 10 numbers per line, but not sure how to go about it.

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>');

}

Upvotes: 0

Views: 83

Answers (1)

dekajoo
dekajoo

Reputation: 2102

Based on Ian Brindley answer it could be :

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;

Upvotes: 2

Related Questions