Вот разбивка того, как ваши функции работают построчно (я включил комментарии после каждого оператора):
function isUniform(x) {
var first = x[0]; //SET "FIRST" to first element in array
for(var i = 1; i < x.length; i++) { //loop from second element to the end
if(first === x[i]) { //if "FIRST" is equal to this element
return true; //conclude that the ENTIRE ARRAY is uniform and quit function
i++; //incremenet "i" (note, the loop automatically does this, so this will result in an extra increment
}
} return false; //conclude the array is not uniform IF THE FIRST ITEM IS UNIQUE
};
Вот разбивка второй функции:
function isUniform(x) {
var first = x[0];//SET "FIRST" to first element in array
for(var i = 1; i < x.length; i++) { //loop from second element to the end
if(x[i] !== first) { //if this element is not equal to the first CONCLUDE THAT THE ARRAY IS NOT UNIFORM and quit function
return false;
i++; //again, extra un-needed increment, but it technically does not matter in this case
}
} return true; //CONCLUDE that since no items were NOT equal to the first item, the array is uniform
};
Таким образом, теперь должно быть ясно, что второй массив выполняет вашу задачу, а первый - нет.Действительно, первый проверяет, равны ли какие-либо элементы, кроме первого, первому.