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

91
Visualizações
javascript global variable undefined when calling inside the new, additional function

Coding linting with JS to iterate through a text. I have a few iterations that count a specific conditions, for example sentences are set to be added upon when there is '.' or '!'.

However, when I created a function that would print one string with information about number of sentences, overused words and so on, it shows up as undefined... Here's a piece of code

console.log(betterWords.length);
let counter = 0;

for (let i = 0; i < betterWords.length; i++){
  if (betterWords[i] === 'really' || betterWords[i] === 'very' || betterWords[i] === 'basically'){
    counter++
}
}
console.log('You used overused words ' + counter + ' times.');

let sentences = 0;

betterWords.forEach (word => {
  if (word[word.length-1] === '.' || word[word.length-1] === '!'){
    sentences++
}
});

console.log('There are ' + sentences + ' sentences');
numberOfWords = betterWords.length;

const printFunction = (betterWords, counter, sentences) => {
  return('The number of words in text is ' + numberOfWords + ' There are ' + sentences + ' and overused words are used ' + counter + ' times.');
};

console.log(printFunction());

Output 182 You used overused words 8 times. There are 12 sentences The number of words in text is 182 There are undefined and overused words are used undefined times. I am mentioning sentences as one example here. One can see that numberOfWords give a proper output

As one can see once sentences return 12, the other time it's undefined.

about 4 years ago · Juan Pablo Isaza
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