I'm trying to write a function that will capitalize the first letter of the first and last name only... any ideas on how to approach this?
const namesHardest = ['emIly sMith', 'angeliNA Jolie', 'braD piTt'];
function capitalizeNames(peopleArray) {
return peopleArray.toString().toLowerCase().split('').map(function(word) {
return (word.charAt(0).toUpperCase() + word.slice(1));
}).join(' ').split();
}
// set the resulting array to this variabble
const capitalizedNames = capitalizeNames(namesHardest);
capitalizedNames;
I'm trying to write a function that will capitalize the first letter of the first and last name only... any ideas on how to approach this?
const namesHardest = ['emIly sMith', 'angeliNA Jolie', 'braD piTt'];
function capitalizeNames(peopleArray) {
return peopleArray.toString().toLowerCase().split('').map(function(word) {
return (word.charAt(0).toUpperCase() + word.slice(1));
}).join(' ').split();
}
// set the resulting array to this variabble
const capitalizedNames = capitalizeNames(namesHardest);
capitalizedNames;
Share
Improve this question
edited May 7, 2018 at 23:23
ibrahim mahrir
31.7k5 gold badges49 silver badges77 bronze badges
asked May 7, 2018 at 22:53
user8227859user8227859
651 silver badge8 bronze badges
11
-
can you see what
.split('')
does? i.e. not what you want – Jaromanda X Commented May 7, 2018 at 22:55 - @JaromandaX I think the problem is even before that with the toString(), no? I just don't know what I could use instead. – user8227859 Commented May 7, 2018 at 22:56
- nope, array.toString will give you the ining array as one string, separated by spaces - though, probably not what you want either - you need to work on each element of the array separately – Jaromanda X Commented May 7, 2018 at 22:57
-
3
something like
return peopleArray.map(name => name.split(' ').map(word => word[0].toUpperCase() + word.slice(1).toLowerCase()).join(' '))
– Jaromanda X Commented May 7, 2018 at 22:59 - something exactly like that. – Stephen P Commented May 7, 2018 at 23:01
4 Answers
Reset to default 7One issue is using array.toString - that results in a string like
'emIly sMith angeliNA Jolie braD piTt'
so, you've lost your array elements
You need to work on each element individually, by using array.map
function capitalizeNames(peopleArray) {
return peopleArray.map(function(name) {
/* do something with each name *
})
}
Your other issue is that split('')
splits a string into characters - but you want to split it on spaces ... i.e. split(' ')
So now, we have
function capitalizeNames(peopleArray) {
return peopleArray.map(function(name) {
return name.split(' ').map(function(word) {
/* do something with each part of name *
});
});
}
so, now, how to capitalise a string - your code works, but I prefer
word[0].toUpperCase() + word.slice(1).toLowerCase();
put it together and you get
function capitalizeNames(peopleArray) {
return peopleArray.map(function(name) {
return name.split(' ').map(function(word) {
return word[0].toUpperCase() + word.slice(1).toLowerCase();
});
});
}
or, in ES2015+, using arrow functions (since your code uses const
, why not use all of ES2015+)
const namesHardest = ['emIly sMith', 'angeliNA Jolie', 'braD piTt'];
const capitalizeNames = peopleArray => peopleArray.map(name =>
name.split(' ').map(word =>
word[0].toUpperCase() + word.slice(1).toLowerCase()
).join(' ')
);
const capitalizedNames = capitalizeNames(namesHardest);
console.log(capitalizedNames);
Sorry I am late to party, I'd rather use array.from with closure
const namesHardest = ['emIly jack sMith', 'angeliNA Jolie', 'braD piTt'];
let conv=Array.from(namesHardest,a=>a.toLowerCase().replace(/\b[a-z]/g, function(letter) {
return letter.toUpperCase();
}))
console.log(conv);
let namesHardest = ['emIly sMith', 'angeliNA Jolie', 'braD piTt'];
namesHardest = namesHardest.map(val => {
let [first, last] = val.toLowerCase().split(' ');
first = first.replace(first[0], first[0].toUpperCase());
last = last.replace(last[0], last[0].toUpperCase());
return `${first} ${last}`
});
console.log(namesHardest);
Your logic is a bit off. First, for each string, you need to split it by spaces to get the first and last name. Then, you can upcase the first character of each string. See below:
const namesHardest = ['emIly sMith', 'angeliNA Jolie', 'braD piTt'];
const capitalizeName = (name) => `${name[0].toUpperCase()}${name.slice(1)}`;
const capitalizeNames = (peopleArray) => peopleArray.map(name => {
const [first, last] = name.toLowerCase().split(' ');
return `${capitalizeName(first)} ${capitalizeName(last)}`;
});
console.log(capitalizeNames(namesHardest))