Home > other >  Trying do write return of a Occurence function
Trying do write return of a Occurence function

Time:09-02

How can I change the function count_occurence() to get the proper out put.

It can count the number of times all string occurs in another string.

This is the function

function count_occurence( text, occurence )
{
  var div = text.toLowerCase().split(/[^a-zA-ZàèìòùÀÈÌÒÙáéíóúýÁÉÍÓÚÝâêîôûÂÊÎÔÛãñõÃÑÕäëïöüÿÄËÏÖÜŸçÇßØøÅåÆæœ]/);
  var obj_list = {};

  for (let word of div) {    
    if (word != "" ) 
    {
      if (obj_list[word] == undefined) {
        obj_list[word] = 1;
      } else {
        obj_list[word]  = 1;
      }
    }
  }
  
  for (var word in obj_list)
  {
    if( obj_list[word]==1 ){
      delete(obj_list[word]);
    }
   
  }
  return obj_list
}

var text = document.querySelector('#randomtxt').textContent;

console.log(count_occurence( text ));
<div id="randomtxt"> 
The red glow of tail lights indicating another long drive home from work after an even longer 24-hour shift at the hospital. The shift hadn’t been horrible but the constant stream of patients entering the ER meant there was no downtime. She had some of the “regulars” in tonight with new ailments they were sure were going to kill them. It’s amazing what a couple of Tylenol and a physical exam from the doctor did to eliminate their pain, nausea, headache, or whatever other mild symptoms they had. Sometimes she wondered if all they really needed was some interaction with others and a bit of the individual attention they received from the nurses.
<br />
<br />
After hunting for several hours, we finally saw a large seal sunning itself on a flat rock. I took one of the wooden clubs while Larry took the longer one. We slowly snuck up behind the seal until we were close enough to club it over its head. The seal slumped over and died. This seal would help us survive. We could eat the meat and fat. The fat could be burned in a shell for light and the fur could be used to make a blanket. We declared our first day of hunting a great success.
<br />
<br />
Love isn't always a ray of sunshine. That's what the older girls kept telling her when she said she had found the perfect man. She had thought this was simply bitter talk on their part since they had been unable to find true love like hers. But now she had to face the fact that they may have been right. Love may not always be a ray of sunshine. That is unless they were referring to how the sun can burn.
</div>

But i don't know how to change my function and get a out put like this

console.log(count_occurence( text,"the" )); //Output expected "20"
console.log(count_occurence( text,"of" )); //Output expected "9"
console.log(count_occurence( text,"from" )); //Output expected "3"

CodePudding user response:

I used the second parameter as a filter, which compares every word with it and when it's true, it's getting passed into the return array.

const randomText = "In linear algebra, a rotation matrix is a transformation matrix that is used to perform a rotation in Euclidean space. For example, using the convention below, the matrix"


function countWords(text, filter = ''){
   const words = {}
   text.split(' ').filter(word => {
      if(typeof filter == 'string' && filter.length > 0){
        return word === filter
      }
      else return true
   }).forEach(word => {
      if(!(Object.keys(words).includes(word))) {
        words[word] = 1
      }
      else words[word]  
   })
   return words
}
console.log(countWords(randomText, 'a'))

CodePudding user response:

const randomText = "In linear algebra, a rotation matrix is a transformation matrix that is used to perform a rotation in Euclidean space. For example, using the convention below, the matrix"
var txt = randomText.toLowerCase()

function countOccurence(text, occurence) {
  var div = text.toLowerCase().split(/[^a-zA-ZàèìòùÀÈÌÒÙáéíóúýÁÉÍÓÚÝâêîôûÂÊÎÔÛãñõÃÑÕäëïöüÿÄËÏÖÜŸçÇßØøÅåÆæœ]/);
 
   const words = {}
   div.filter(word => {
      if(typeof occurence == 'string' && occurence.length > 0){
        return word === occurence
      }
      else return true
   }).forEach(word => {
      if(!(Object.keys(words).includes(word))) {
        words[word] = 1
      }
      else words[word]  
   })
   return words
}
console.log(Object.values(countOccurence("is")))
For "is " expect 2 got 1

CodePudding user response:

You passed 'is' as text, the filter is second parameter

const randomText = "In linear algebra, a rotation matrix is a transformation matrix that is used to perform a rotation in Euclidean space. For example, using the convention below, the matrix"
var txt = randomText.toLowerCase()

function countOccurence(text, occurence) {
  var div = text.toLowerCase().split(/[^a-zA-ZàèìòùÀÈÌÒÙáéíóúýÁÉÍÓÚÝâêîôûÂÊÎÔÛãñõÃÑÕäëïöüÿÄËÏÖÜŸçÇßØøÅåÆæœ]/);
 
   const words = {}
   div.filter(word => {
      if(typeof occurence == 'string' && occurence.length > 0){
        return word === occurence
      }
      else return true
   }).forEach(word => {
      if(!(Object.keys(words).includes(word))) {
        words[word] = 1
      }
      else words[word]  
   })
   return words
}
console.log(Object.values(countOccurence(randomText, "is")))

  • Related