Codility 2.1

by Igor Cuckovic

JavaScript

var A = [ 3, 2, 5, 4, 6, 7, 8, 9];

function solution(A) {
    // you can use console.log for debugging purposes, i.e.
    // console.log('this is debug message');
    // write your code in JavaScript (ECMA-262, 5th edition)

    var A1 = A.sort(function (a, b) {
        return a - b;
    });
    
    if (A1.length === 0) {
        return 1; //"Array is empty!";
    }

    if (A1[0] !== 1) {
        return 0;
    }
    
    
    for (var i = 0; i < (A1.length - 1); i += 1) {

        if ((A1[i + 1] - A1[i]) !== 1) {
            return 0;
        } 
    }

    return 1;
}

console.log(solution(A));