Reputation: 213
I've been trying to figure this out for a good while now, and I can't solve it. This function receives an array of names and should return an array containing the middle names. If someone does not have a middle name the array should contain the value null at this index.
I know my code isn't complete, but cannot make work even half of it. The splice method doesn't work whatever I do, and now I'm very lost.
This is my code so far:
function getMiddleNames (names) {
let solution =[];
let separated = [];
for(let i=0; i<names.length; i++){
separated.push(names[i].split(' '));
}
return separated.splice(1);
}
And the tests:
describe('getMiddleNames', function () {
it('returns [] when passed []', function () {
expect(getMiddleNames([])).to.eql([]);
});
it('returns ["Hannah"] when passed ["Caroline Hannah Jamieson"]', function () {
expect(getMiddleNames(['Caroline Hannah Jamieson'])).to.eql(['Hannah']);
});
it('returns ["Reuben", "Keith", "Clara"] when passed ["Steven Reuben Williams", "Carl Keith Morelli", "Sissel Clara Blomqvist"]', function () {
expect(getMiddleNames(['Steven Reuben Williams', 'Carl Keith Morelli', 'Sissel Clara Blomqvist'])).to.eql(['Reuben', 'Keith', 'Clara']);
});
it('returns ["Reuben", null, "Clara"] when passed ["Steven Reuben Williams", "Carl Morelli", "Sissel Clara Blomqvist"]', function () {
expect(getMiddleNames(['Steven Reuben Williams', 'Carl Morelli', 'Sissel Clara Blomqvist'])).to.eql(['Reuben', null, 'Clara']);
});
it('returns ["Reuben", null, null] when passed ["Steven Reuben Williams", "Carl Morelli", "Sissel"]', function () {
expect(getMiddleNames(['Steven Reuben Williams', 'Carl Morelli', 'Sissel'])).to.eql(['Reuben', null, null]);
});
});
Could anyone give me a hand? Thank you
Upvotes: 1
Views: 871
Reputation: 320
try this:
function getMiddleNames(names) {
return names.map(x => getMiddleNameOf(x));
}
function getMiddleNameOf(name) {
let split = name.split(' ');
return split.length == 3 ? split[1] : null;
}
Upvotes: -1
Reputation: 92460
Your pushing the whole name into your separated array and then splicing that. Which doesn't really work. You need to separate the names and split each one individually:
function getMiddleNames (names) {
let solution =[];
for(let i=0; i<names.length; i++){
let separated = names[i].split(' ')
solution.push(separated.length == 3 ? separated[1] : null);
}
return solution
}
let names = ['Steven Reuben Williams', 'Carl Morelli', 'Sissel Clara Blomqvist']
console.log(getMiddleNames(names))
This of course assumes there will always be a middle name. It will fail and deliver the last name without it.
You can be a bit more succinct with map
and test for name length at the same time. Still there will be edge cases (people with four names, etc):
let names = ['Steven Reuben Williams', 'Carl Keith Morelli', 'Sissel Clara Blomqvist']
let middles= names.map(n => {
s = n.split(' ')
// s should be an array [first, middle, last]
// if no middle name it will be [first, last] and length 2
return s.length == 3 ? s[1] : null
})
console.log(middles)
Upvotes: 4