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

Thursday, December 15, 2016

Codefights isSmooth(arr)


//sample solution
function isSmooth(arr) {
    //if array is odd
    //document.writeln(arr.length,"<br/>");
    var firstElement = arr[0];
    var lastElement = arr[arr.length-1];
    if(arr.length%2 === 0){
    //document.writeln("EVEN","<br/>");
        var middleElementUpperIndex = Math.floor(arr.length/2);
        var middleElementLowerIndex = middleElementUpperIndex - 1;
        var middleElementLower = arr[middleElementLowerIndex];
        var middleElementUpper = arr[middleElementUpperIndex];
        var middleElementSum = middleElementLower + middleElementUpper;
        if(firstElement == lastElement && lastElement == middleElementSum){
        return true;
        } else {
        return false;
        }
    } else {
    //document.writeln("ODD","<br/>");
       
        var middleElementIndex = Math.floor(arr.length/2);
        //document.writeln("middle element index is ", middleElementIndex, "<br/>");
        var middleElement = arr[middleElementIndex];
        //document.writeln("middle element is ", middleElement, "<br/>");
        if(firstElement == lastElement && lastElement == middleElement){
        return true;
        } else {
        return false;
        }
    }
   
}

No comments:

Post a Comment


This is an example of scrolling text using Javascript.

Popular Posts