Skip to content

3. Highlight Overused Word in Color

Playback Speed:
Transcript

By now, you have written code that identifies one overused word in a document.

In this video, you will write code that highlights that word in red, so you can easily find the writing mistake.

In the last video, you set the variable “searchResult” to contain the overused word "very".

Next, add code that sets the background color to highlight the word.

Beneath the logger function, type “searchResult.getElement.”

Use the autofill menu to help.

Then, type “.asText.”

Finally, type “.setBackgroundColor” just as you see on the screen.

The "set background color" function includes placeholders for the parameters the function needs to work.

Fill in values for those parameters.

The first two parameters require start and end numbers to determine where the highlight will begin and end.

Change “startOffset" to “searchResult.getStartOffset” using the autofill menu for help.

Then, change “endOffset inclusive” to “searchResult.getEndOffsetInclusive,” just like you see on the screen.

Finally, specify a parameter for the background color.

Save the code, and test it.

An error message appears.

This error means that you must specify a parameter for "color," so the program knows which color to use to highlight the text.

To highlight the text in red, replace the word “color” with the hex code for the color red, and put double quotes around it.

If you’d like to highlight with a different color, search the internet for “hex color picker.”

Select the color you like, then enter the hex code into your program.

Click the link on this page to choose a different color and get its hex code.

Run your program.

If you get an error message, check your code carefully.

Make sure each parameter is entered correctly, and that you have double quotes around the color code.

Now, check out the result in your document.

Click on the tab for your Auto Editor document.

The first use of the word “very” is highlighted in the color you chose!

The code worked!

Add a comment to your code so you will remember what this new line of code does.

If you have trouble getting your code to work, double check that your code matches the example, especially the capital letters and punctuation marks.

If you get stuck, ask your neighbor for help.

You have completed step two of this activity!

In the next video, you will add to the program so it finds all the times the word “very” appears in the document.

Now, it’s your turn.

Use the “setBackgroundColor” function to highlight the word "very" in your document.

Instruct the function to use the start and end positions of the searchResult variable.

Add the color code for red as the last parameter, or search for a different color code.

Then, test your code and check out the results.

Finally, add a comment to your program, so you can easily remember what this new line of code does.

When you’re done, move on to the next video.

Next
Instructions
  1. Use the .setBackgroundColor function to highlight the word "very" in your document.
  2. Use the start and end position of searchResult as parameters.
  3. Add the color code for red as the last parameter.
  4. Test your code between each step.
  5. Add a comment to your code.