0

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

}
4
  • 3
    text += points[i] + ((i % 10 === 0) ? '<br />' : ''); Commented Sep 28, 2014 at 15:27
  • definitely a time to use modulo Commented Sep 28, 2014 at 15:28
  • 1
    love modulo, love ternary Commented Sep 28, 2014 at 15:29
  • var points = ... don't need to be in the loop neither does document.getElementById("demo").innerHTML = points; Commented Sep 28, 2014 at 15:29

1 Answer 1

2

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;
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you this is helpful to me, one quick question. How do I get the "," to display between each number. As it is now like this when the sort is displayed. 100 99989796959493929190 89888786858483828180 79787776757473727170 69686766656463626160 59585756555453525150 49484746444342414039 38373736353433323130 29282726252423222120 19181716151413121110 987654321
I have modified my answer to have a "," between numbers. You should accept the answer if it answers your question so that it can help people with the same issue : )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.