Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

220
Visualizações
How can I capitalize the necessary letters to return properly capitalized sentences? JavaScript

This is what I have. Any suggestions would greatly help. Thanks!:

function fixGrammar(input) {
  for (let i = 0; i < input.length; i++) {
    if(i === 0){
      input = input[i].toUpperCase() + input.slice(1);
    }  if(input[i] === (/\s[i+ ]/g)) {
        let capitalize = input[i].toUpperCase()
    } if(input[i] === '.') {
        let afterPeriod = input[i+2];
        afterPeriod.toUpperCase();
      }
    }
  return input;
  }

console.log(fixGrammar("there's something you learn in your first boot-camp. if they put you down somewhere with nothing to do, go to sleep — you don't know when you'll get any more." ))
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You cannot compare a letter with a regex using == and hope to find a match. Also that regex /\s[i+ ]/g would look for white space followed by a literal i or literal + or a space.

You can use a regex to spot the letters that need to be capitalized, and replace them with replace:

const fixGrammar = s => s.replace(/(?<=^|\. )\w/g, c => c.toUpperCase());

console.log(fixGrammar("there's something you learn in your first boot-camp. if they put you down somewhere with nothing to do, go to sleep — you don't know when you'll get any more." ));

about 4 years ago · Juan Pablo Isaza Relatório

0

One liner solutions FTW:

Sol 1:

input.split(". ").map(
  m => 
    m[0].toUpperCase() + m.slice(1) // `m[0]` gets the first character, `m.slice(1)` skips the first character and collects the rest of them together.
 ).join(". ")

Sol 2: Or you may make it more verbose:

input.split(". ").map(
    ([firstChar, ...restChars]) => 
       firstChar.toUpperCase() + restChars.join("") // `restChars` would be an array of characters, so we are joining them to get the original substring
    ).join(". ")

In the first sol, m.slice(1) will return string, but if you were to do the same for array, lets say ["a", "b", "c"].slice(1); it would return a new array ["b", "c"].


PS: input is the variable which has the string value. You may use it inside your fixGrammar function. Solution mentioned here assumes that there would be exactly one space after the dot!

about 4 years ago · Juan Pablo Isaza Relatório

0

Split on periods with any number of trailing spaces, uppercase the first character of each sentence, then join the array on a period and a space.

const paragraph = `there's something you learn in your first boot-camp. if they put you down somewhere with nothing to do, go to sleep — you don't know when you'll get any more.`;

const capitalizeSentences = (str) => {
    return str
        .split(/\.[\s]+/)
        .map((sentence) => `${sentence[0].toUpperCase()}${sentence.slice(1)}`)
        .join('. ');
};

console.log(capitalizeSentences(paragraph));

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda