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

112
Visualizações
only get the last result in a for loop js

so i wanted my for loop to run trough a text, and count how many times the words: 'really', 'basically' and 'very' were mentioned.

here is the text:

let story = 'Last weekend, I took a very beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It's really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some very artsy photos. It was a really short stop, though, because I had a really long way left to go.' ;

let storyWords= story.split(' ');

this is the function:

function count() {

let reallycount = 0;
let verycount =0;
let basicallycount = 0;
    
    for (let i = 0; i < storyWords.length; i++) {
        if (storyWords[i] === 'really'){
            reallycount += 1;
            console.log ('really count :' + reallycount);
        } else if ( storyWords[i] === 'very'){
            verycount += 1;
            console.log ('very count :' + verycount);
        } else if ( storyWords[i] === 'basically'){
            basicallycount += 1;
            console.log ('basically count :' + basicallycount);
        }
    } 
} count();

The results:

very count :1, really count :1, basically count :1, very count :2, really count :2, really count :3,

My question: so i only want the last result of very-, bascially-, and reallycount to show. in other words i want the results to be:

basically count :1, very count :2, really count :3,

how can i hide/remove the other results?

about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Here is a slightly more universal form of your counts() function that works more in a functional way:

const story = `Last weekend, I took a very beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It's really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some very artsy photos. It was a really short stop, though, because I had a really long way left to go.` ;

function counts(str, words){
  const arr=str.toLowerCase().split(/\s+/);
  return arr.reduce((a,w)=>{
if(words.indexOf(w)>-1) a[w]=(a[w]||0)+1
return a
  }, {} )
}

console.log(counts(story,["really","actually","very"]));

about 4 years ago · Santiago Trujillo Relatório

0

You can extract it to variables and console log it afterwards

function count() {
    let reallycount = 0;
    let verycount = 0;
    let basicallycount = 0;

    for (let i = 0; i < storyWords.length; i++) {
        if (storyWords[i] === "really") {
            reallycount += 1;
        } else if (storyWords[i] === "very") {
            verycount += 1;
        } else if (storyWords[i] === "basically") {
            basicallycount += 1;
        }
    }
    console.log("basically count :" + basicallycount);
    console.log("very count :" + verycount);
    console.log("really count :" + reallycount);
}
count();
about 4 years ago · Santiago Trujillo Relatório

0

Another option for counting the number of times a word occurs is by using the filter function

const story = "Last weekend, I took a very beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It's really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some very artsy photos. It was a really short stop, though, because I had a really long way left to go."

const storyWords = story.split(" ")

function count() {
    const basicallycount = storyWords.filter(word => word === "basically").length
    const verycount = storyWords.filter(word => word === "very").length
    const reallycount = storyWords.filter(word => word === "really").length

    console.log(`basically count: ${basicallycount}`
    console.log(`very count: ${verycount}`
    console.log(`really count: ${reallycount}`
}

count();
about 4 years ago · Santiago Trujillo 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