- This topic is empty.
-
AuthorPosts
-
May 16, 2016 at 10:10 pm #241771mreynolds2016Participant
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” 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>May 17, 2016 at 5:01 am #241780JerbaParticipantYou 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; });
-
AuthorPosts
- The forum ‘Other’ is closed to new topics and replies.