Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!