JSFiddle - React, Tailwind, and code Playground

by r3wt

HTML

<strong>Outputs the biggest number for N inputs </strong><br/>
Input some numbers, seperated by a comma:</br>
<input type="text" id="input" value="9,11,118" onkeyup="test(this)" /></br>
Output:</br>
<input type="text" value="0" id="output" readonly />

JavaScript

//here is the function
function makeBiggestPossibleNumber(){

    var list = Array.prototype.slice.call(arguments);

    var greatest = ~~list.join('');// join array as string then convert to integer.

    for(var i=0;i<list.length;i++){
        var a = i+1;
        //if i == last item in array, we need to wrap around
        if(i+1 == list.length){
            a = 0;
        }
        var b = list[i];
        list[i] = list[a];
        list[a] = b;
        var test = ~~list.join('');//same as above
        if(test > greatest){
            greatest = test;
        }
    }
    return greatest;
}

//below is just code for the demo
var timeout = null;
var output = document.getElementById('output');
window.test = function( element ){
    clearTimeout(timeout);
    timeout = setTimeout(function(){
        var nums = element.value.split(',');
        var result = makeBiggestPossibleNumber.apply(null,nums);
        output.value = result;
    },200);
};

window.documentReady = function( fn ){
    var docLoaded = setInterval(function(){
        if(document.readyState !== "complete"){
        		return;
        }
        clearInterval(docLoaded);
        fn.apply(window);
    }, 30);
};

documentReady(function(){
    test(document.getElementById('input'));
});