I'm currently working on a function called 'every' that takes in an array and a callback function as arguments. The purpose of the callback function is to determine if all elements in the array meet a certain condition. In my case, I want the function to return true if all elements are strings, and false otherwise.
Here is what I have so far:
function every(myArray, callback) {
var myNewArray = [];
for (var i = 0; i < myArray.length; i++){
var added = callback(myArray[i]);
myNewArray.push(added);
}
if (myArray === 'string') {
return true;
} else {
return myNewArray
}
}
function isTrueOrFalse(string) {
if (typeof string === 'string') {
return true;
} else {
return false;
}
}
var result = every(
['5','test true','truthy statement','stringy'],
isTrueOrFalse
);
console.log(result);
As it stands now, the function returns a boolean value for each element in the array: 'false'
, 'true'
, 'true'
, 'true'
However, I only want it to return one false
and stop there. What am I missing?