I recently started coding and came across this problem that I'm struggling to solve.
The task at hand is to create a function that takes an array of full names and returns only those with the last name "Smith".
Example: ['Charlotte Jones', 'Rebecca Smith', 'Harry Smith', 'John Smithy', 'Smith Jones'] => ['Rebecca Smith', 'Harry Smith']
Here's the code I've written so far:
function getSmiths(arr) {
return arr.filter(a =>a.includes("Smith"))
}
console.log(getSmiths(['Charlotte Jones', 'Rebecca Smith', 'Harry Smith', 'John Smithy', 'Smith Jones']));
I've tested my code with the following cases:
describe("getSmiths", () => {
it("returns [] when passed []", () => {
expect(getSmiths([])).to.eql([]);
});
it("returns a Smith from a mixed arrau", () => {
expect(getSmiths(["Harry Smith", "Charlotte Bank"])).to.eql([
"Harry Smith"
]);
});
it("returns multiple Smiths from a mixed array", () => {
expect(getSmiths(["Harry Smith", "Charlotte Bank"])).to.eql([
"Harry Smith"
]);
});
it("ignores Smiths found in first names", () => {
expect(getSmiths(["Smithy Jones", "Harry Smith"])).to.eql(["Harry Smith"]);
});
it("ignores Smiths found in extended last names", () => {
expect(getSmiths(["John Smith", "Chris Smithy"])).to.eql(["John Smith"]);
});
});
Can anyone offer any insights as to why my code isn't working? Any suggestions would be greatly appreciated!