Lesson 4 of 4
Objects
An object groups named values (called properties) together:
Counting with objects
Objects are perfect for tallying things. If a key doesn't exist yet, it's undefined, so ?? 0 gives a starting value:
Arrow functions
Arrow functions are a shorter way to write small functions:
Exercise: Word counter
Write countWords(text) that returns an object counting how many times each word appears. Words are separated by single spaces.
For example, countWords("the cat the hat") returns { the: 2, cat: 1, hat: 1 }.
Your turn!
Write your code in and press Submit answer.
- console.log(JSON.stringify(countWords("the cat the hat"))); → {"the":2,"cat":1,"hat":1}
- console.log(JSON.stringify(countWords("ale"))); → {"ale":1}
- plus 1 hidden check