Quotes help make search much faster. Example: "Practice Makes Perfect"

Thursday, December 15, 2016

Codefights makeArrayConsecutive2(sequence)


//sample solution
function makeArrayConsecutive2(sequence) {
    sequence.sort(function(a,b){return a - b}) //for correct number sorting
    //document.writeln("sorted: ", sequence, "<br/>");
    var lowestElement = sequence[0];
    var highestElement = sequence[sequence.length-1];
    var properSequence = [];
    if(highestElement - lowestElement >= 0){
    for(i=lowestElement; i<=highestElement; i++){
    properSequence.push(i);
    }
    } else {
    for(i=lowestElement; i>=highestElement; i--){
    properSequence.push(i);
    }
    }
    //document.writeln("proper: ", properSequence, "<br/>");
    var numberOfHoles = properSequence.length - sequence.length;
    return numberOfHoles;
}

No comments:

Post a Comment


This is an example of scrolling text using Javascript.

Popular Posts