skip to Main Content

In the function below it takes a sentence as an argument and counts the total occurrence of letters and return them as an object. The letter being the key and total number of occurrence as the value. We are starting off with an empty object. I am getting confused how the object get populated as it started empty. My confusions are commented in the code.

const wordCounter = (sentence) => {
  let words = {}

  // Words object is empty here
  for (letter of sentence) {
    
    // Words object is still empty here right?
    if (letter in words) {
      
      // How did the letters automatically gets stored in
      // the words object when they were previously empty
      // on line 3 without calling any sort of function
      // to do the storing?
      words[letter]++
    } else {
      words[letter] = 1
    }
  }
  return words
}

console.log(wordCounter('Help me with this I am confused'))
/*Output: { 
 :  6
 H: 1
 I: 1
 a: 1
 c: 1
 d: 1
 e: 3
 f: 1
 h: 2
 i: 2
 l: 1
 m: 2
 n: 1
 o: 1
 p: 1
 s: 2
 t: 2
 u: 1
 w: 1}
 */

2

Answers


  1.     //first we define the function and pass "sentence" as an argument
        const wordCounter = (sentence)=>{
          //here we define the object words
          let words = {}
          //the for of loop then cycles through each letter of the sentence 
          //one by one
          for(letter of sentence){
            //Words object is still empty here right?
            //answer to ^^^ : only on the first iteration of the loop
        
            //here we define a conditional statement asking if the letter which is 
            //currently being checked has been deposited into the words object
            if(letter in words)
              //if the letter is in the words object then add 1 using the increment operator
              words[letter]++
            }else{
              //if the letter does not exist in the object place it here as the 
              //key, and make the value equal 1
              words[letter] = 1
            }
          }
          //return the words object
          return words
        }
    

    I hope i was able to make some sense of this for you.

    Login or Signup to reply.
  2. Let’s trace the capital letter "H" – your first letter.

    // Currently the words object is empty
    // Does capital H **already** exist in the object?
    // (Note the emphasis on "already")
    if (letter in words) {
    
      // No it doesn't already in the object
      // So we skip this expression
      words[letter]++;
    } else {
    
      // Because "H" doesn't already exist but
      // we want to add it we create a new key "H"
      // and set its value to 1 because
      // we've only one encountered one instance of "H"
      // at this point in the loop
      words[letter] = 1;
    }
    

    From this point, if we have another "H", we use the first expression to increment the value of "H" that we added during that first iteration.

    Small note: "words" might be the wrong variable name to use for the object. "letters" or "lettersDictionary" might be more appropriate since you’re storing (a dictionary of) letters not words.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search