Consider the code snippet below:
function isUniform(myArray) {
myArray.forEach(function(element) {
if (element !== myArray[0]) {
return false;
}
});
return true;
}
The intention is that the function should take an array as input and return true if all the elements in the array are the same and false otherwise.
eg:
isUniform([1,2,1,1]); // should return false isUniform([1,1,1,1]); // should return true
However, the if condition:
if (element !== myArray[0])
never seem to be true in the case of isUniform([1,2,1,1]).
What is it that I am missing ?
Aucun commentaire:
Enregistrer un commentaire