Good day. I can't get the sorting animation to work correctly. At this stage, stopped adding elements to the DOM
function getRandomArray() { var a = []; function getRandom() { return parseInt(Math.random() * 10); }; for (var i = 0; i < 10; i++) { a.push(getRandom()); }; return a }; function bubbleSortAnimation(list) { var animSteps = []; function printArray(list) { var str = [], i = 0, l = list.length for (i; i < l; i++) { str.push('<li>' + list[i] + '</li>') }; $('#not-sorted').html(str.join('')); }; function setSwap(list, itemFist, itemSecond) { var tmp = list[itemFist]; list[itemFist] = list[itemSecond]; list[itemSecond] = tmp; animSteps.push(function() { $('#not-sorted').children('li').eq(itemFist).add($('#not-sorted').children('li').eq(itemSecond)).addClass('highlight'); }, function() { var tmp = $('#not-sorted').children('li').eq(itemFist).text(); $('#not-sorted').children('li').eq(itemFist).text($('#not-sorted').children('li').eq(itemSecond).text()); $('#not-sorted').children('li').eq(itemSecond).text(tmp); }, function() { $('#not-sorted').children('li').eq(itemFist).add($('#not-sorted').children('li').eq(itemSecond)).removeClass('highlight'); }); }; function animation() { if (animSteps.length) { setTimeout(function() { animSteps.splice(0, 1)[0](); animation(); }, 500); } }; function sort(list) { for (var j = list.lenght; j > 1; --j) { for (var i = 0; i < j - 1; i++) { if (list[i] > list[i + 1]) { setSwap(list, i, i + 1); } } } }; };