JavaScript

Lesson 11 of 11

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(), and text.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

  1. Task 1, not checked yet. Write countWords(text). It returns the number of words in text. Words are separated by one space. An empty text has 0 words.

  2. Task 2, not checked yet. Write longestWord(text). It returns the longest word in text. When two words have the same length, return the first one.

  3. 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'.

  4. Task 4, not checked yet. Write isPalindrome(text). It returns true when the letters of text read the same forward and backward. Ignore capital letters and spaces.

  5. 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 }.

  6. Task 6, not checked yet. Use your functions on the sentence The quick brown fox jumps over the lazy dog. Print two lines: Words: 9 and Longest: 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.

All tasks

main.js

Ctrl+Enter runs your code. Ctrl+Shift+Enter checks it. Press Esc, then Tab, to leave the editor.

Output

Press Run to see your result

Your output shows here. Press Run to try your code.