lundi 4 juin 2018

Logical issue with Arrays and For loop JS

guys!
There is clearly something wrong with my logic or with the logic of JS (haha).
I really don't understand why one of them works and another one doesn't.
These functions are for checking if every single index in the array is the same. The first one works, and the second one doesn't and I don't see how the logic of these two are different (besides the obvious point of changing the positions).
1.

function isUniform(x) {
    var first = x[0];
    for(var i = 1; i < x.length; i++) {
        if(first === x[i]) {
            return true;
            i++;
        }
    } return false;
};  

2.

function isUniform(x) {
    var first = x[0];
    for(var i = 1; i < x.length; i++) {
        if(x[i] !== first) {
            return false;
            i++;
        }
    } return true;
};

Aucun commentaire:

Enregistrer un commentaire