Issue is you are not returning anything from callback.
Anonymous functions using arrow function can be written as
() = > expression
or
() => { function body; }
Approach 1 by defaults return the value of expression and approach 2 by defaults return undefined
.
Since you are using second approach, you will either have to manually return value or switch to first approach.
Another problem in your code is expression. When you have to test using a regex, you should use method test
.
So your expression should be /^a/i.test(word)
.
If you want to compare first character only, you can so word[0] === "a"
but this will be case sensitive.
Another optimization can be, since you want to return true
if array is empty, you can return an expression that evaluates as:
- Either array should be blank or all words should start with some characters.
So your final code would be:
var allStartingWithA = function(words) {
return words.length === 0 || words.every((word) => /a/i.test(word));
};
console.log(allStartingWithA([]));
console.log(allStartingWithA(["abbb", "aee", "aee", "avvv"]));
console.log(allStartingWithA(["bbb", "aee", "aee", "avvv"]));
References: