i'm trying to create a function that take an array as an argument, and return "true" only if all the items inside the array are the same .
i try to use for loop and i try to use forEeach loop
the first one work great.
the second one not!.
why?
this my first code:
function isUniform(ary) {
var first = ary[0];
for (i = 0; i < ary.length; i++) {
if (first !== ary[i]) {
return false;
}
}
return true;
}
console.log(isUniform([1, 2, 1]));
this my second one:
function isUniform(ary) {
var first = ary[0];
ary.forEach(function(element) {
if (first !== element) {
return false;
}
});
return true;
}
console.log(isUniform([1, 2, 1]));
forEachand return of an inner closure doesn't return to the outer one