Project ยท 25 min
Project: text toolkit
The goal
You will write a small toolkit of five functions that work on text. This project uses strings, if, loops, arrays, objects, and functions together.
Work in order. Each task adds one function. Test each function yourself with console.log before you press Check my work. For example:
console.log(countWords('one two three')); // 3
Tools you can use
These are the tools from the lessons, and two new ones:
text.split(' ')makes an array of the parts of a string.text.split('')makes an array of single letters.array.join(' ')makes a string from an array.array.reverse()reverses the order of an array. It changes the array and gives it back.text.toLowerCase(),text.toUpperCase(), andtext.slice(start).
Tips
- Break each job into small steps. Write one step, print the result, and check it.
- If a check fails, the message shows the arguments and the value that your function gave back.
- There are many ways to solve each task. The checks only look at what your function returns.
After the tasks
Try to extend the toolkit. For example, write a function that finds the most common word, or one that removes punctuation.
Tasks
6 to do
Task 1, not checked yet. Write
countWords(text). It returns the number of words intext. Words are separated by one space. An empty text has0words.Task 2, not checked yet. Write
longestWord(text). It returns the longest word intext. When two words have the same length, return the first one.Task 3, not checked yet. Write
capitalizeWords(text). It makes the first letter of each word a capital letter.capitalizeWords('hello big world')returns'Hello Big World'.Task 4, not checked yet. Write
isPalindrome(text). It returnstruewhen the letters oftextread the same forward and backward. Ignore capital letters and spaces.Task 5, not checked yet. Write
wordFrequency(text). It returns an object that counts each word, in lower case.wordFrequency('a b a')returns{ a: 2, b: 1 }.Task 6, not checked yet. Use your functions on the sentence
The quick brown fox jumps over the lazy dog. Print two lines:Words: 9andLongest: quick.
Task 1 of 6
Write countWords(text). It returns the number of words in text. Words are separated by one space. An empty text has 0 words.