Answer the question
In order to leave comments, you need to log in
Can you please explain what is happening in that function?
Good afternoon
There is a code that capitalizes every first letter in a phrase
const word = (str) => {
let result = '';
for (let i = 0; i < length(str); i += 1) {
console.log(str[i]);
const world = str[i] !== ' ' && (i === 0 || str[i - 1] === ' ');
result += world ? toUpperCase(str[i]) : str[i];
}
return result;
};
document.write(word('просто рандомная фраза'));
const world = str[i] !== ' ' && (i === 0 || str[i - 1] === ' ');
result += world ? toUpperCase(str[i]) : str[i];
const word = (str) => {
let result = '';
for (let i = 0; i < str.length; i += 1) {
console.log(str[i]);
const current = str[i] !== ' ' && (i === 0 || str[i - 1] === ' ');
result += current ? str[i].toUpperCase() : str[i];
}
return result;
};
document.write(word('просто рандомная фраза'));
Answer the question
In order to leave comments, you need to log in
// это условие, при котором букву надо делать большой
const world = str[i] !== ' ' && (i === 0 || str[i - 1] === ' ');
// здесь добавляется либо заглавная, либо просто-как-есть буква,
// в зависимости от условия выше
result += world ? toUpperCase(str[i]) : str[i];
str[i] !== ' '
i === 0
OR the previous character is a space str[i - 1] === ' '
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question