Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

114
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda