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

168
Visualizações
How do I write JavaScript that checks the number of chars in a sentence?

I want to write a script that checks the number of characters (excluding whitespace) in a sentence. For example: "this is a sentence" would be 15 characters. If the sentence contains a break like '.' it should be counted as a different sentence.

I have started writing the script but I think I need some help.

function(sen) {
  return sen.split('.').forEach(function(part) { part.match(/\s*/).result })
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Split by a period and use map to extract the length property:

function check(sen) {
  return sen.split('.').map(e => e.length)
}
console.log(check("Hello. World"))

If you only want the length of the first sentence, split by a period, get the first item and return its length property:

function check(sen) {
  return sen.split('.')[0].length;
}
console.log(check("Hello. World"))

about 4 years ago · Juan Pablo Isaza Relatório

0

const str = 'this is first sentence. this is second sentence';
let sentence = str.split('.');
let result = {};
sentence.forEach((value, index) => {
  result[index + 1] = value.length;
});
console.log(result);

The output will contain character length with respect to the sentence number.

about 4 years ago · Juan Pablo Isaza Relatório

0

(Discounting emojis) Use a regex replace on the whitespaces so you clear them out first..

sen.replace(/\s*/g, '')

and then split the sentence and count the number of characters

function sentenceCounter(sen) {
  return sen.replace(/\s*/g, '').split('.').map(e => e.length)
}

Thats all you should need really, this should return a list of numbers corresponding to the length of your sentences

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