Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other Var Shuffle array…I need to have my array NOT shuffle..help!

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #241771
    mreynolds2016
    Participant

    I have a ” var shuffleArray = function(array) {” that shuffles numbers. I have to make it so that the numbers will NOT shuffle, but will put the numbers on the page in ASCENDING order. HELP!!! My code: <script>$(function() {
    var shuffleArray = function(array) {
    return array.sort(function() {
    return 0.5 – Math.random()
    });
    };
    $(‘head’).append(‘<link rel=”stylesheet” type=”text/css” href=”https://s3-us-west-1.amazonaws.com/kz-public/dtaniwaki/exam/tech-support/001/main.css&#8221; charset=”utf-8″>’)
    setTimeout(function() {
    var numbers = shuffleArray([1, 2, 3, 4, 5, 6, 7, 8, 9]);
    var $target = $(‘#target’);
    var $numbers = $(‘

    <

    div>’).addClass(‘numbers’);
    $target.html($numbers);
    for (var i = 0; i < numbers.length; i += 1) {
    var signature = (“000000000” + Math.floor(Math.random() * 100000000)).slice(-8);
    var $number = $(‘

    ‘ + numbers[i] + ‘

    ‘).addClass(‘number’).addClass(‘row-‘ + Math.floor(i / 3)).addClass(‘column-‘ + (i % 3)).data({
    ‘idx’: i,
    ‘number’: numbers[i],
    ‘signature’: signature,
    ‘state’: ‘number’
    }).appendTo($numbers);
    $number.click(function() {
    var state = $(this).data(‘state’);
    if (state == ‘number’) {
    $(this).text($(this).data(‘idx’) + 1).css(‘font-weight’, ‘bold’).data(‘state’, ‘index’);
    } else {
    $(this).text($(this).data(‘number’)).css(‘font-weight’, ‘normal’).data(‘state’, ‘number’);
    }
    });
    }
    $target.append(‘

    <

    div>’);
    $target.append(‘

    <

    div>’);
    $target.append(‘

    <

    div>’);
    $(‘body’).addClass(‘applied’);
    $(document).trigger(‘lets-start’);
    }, 1000);
    });</script>

    #241780
    Jerba
    Participant

    You can use the native Array.sort function to order array values.

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

    var numbers = [1, 9, 3, 6, 7, 11, 45, 194];
    
    var ASC = numbers.sort(function(previous, current) {
      return previous - current;
    });
    
    var DESC = numbers.sort(function(previous, current) {
      return current - previous;
    });
    
Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘Other’ is closed to new topics and replies.