JSFiddle - React, Tailwind, and code Playground
HTML
<div id="results">Trials remaining: <span id="trial">n/a</span></div>
JavaScript
var trial, sorted, i, j, tmp,
labels = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split(""),
input = jQuery.map( labels, function( text ) {
return jQuery("<li/>").text( text );
}),
nullSort = function( a, b ) { return 0; };
for ( trial = 99; trial >= 0; trial-- ) {
// update counter
jQuery("#trial").text( trial );
// shuffle input
for ( i = labels.length - 1; i > 0; i-- ) {
j = Math.floor( Math.random() * (i + 1) );
tmp = input[ i ];
input[ i ] = input[ j ];
input[ j ] = tmp;
}
sorted = input.slice( 0 );
// "sort"
sorted.sort( nullSort );
// compare
if ( jQuery.map( input, jQuery.text ).join("") !==
jQuery.map( sorted, jQuery.text ).join("") ) {
// dump results and quit
jQuery("<ul/>").appendTo("#results").append(
jQuery.map( sorted, function( el, i ) {
var expected = jQuery.text( input[i] );
return jQuery.text( el ) === expected ?
el :
jQuery( el ).clone().append(
" <em>(expected " + expected + ")</em>"
);
})
);
break;
}
}