Home > Blockchain >  only get the last result in a for loop js
only get the last result in a for loop js

Time:12-24

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?

CodePudding user response:

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();

CodePudding user response:

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"]));

CodePudding user response:

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();
  • Related