Skip to content

5. Make a Function to Find More Words

Playback Speed:
Transcript

In this video, you will reuse the code you created to make your program highlight more words.

By the end of this video, you will have completed all four steps for building the first part of your Auto Editor.

First, change the name of "myFunction" to "findText."

Then, add a new function called "highlightProblem."

Press “enter” a few times after the last curly brace, type “function,” space, “highlightProblem” and a set of parentheses.

Then, type an open curly brace to start the new function.

Save the code.

The function dropdown now contains two options.

Click the “findText” function to highlight the word "very" in the document.

When a computer program runs code, it “calls” functions to complete a set of instructions.

Tell the “highlightProblem” function to "call" the "findText" function.

Type “findText” and a set of open and closed parentheses.

Save the code.

Clear the highlighting from your document, then run the “highlightProblem” function.

Great, it works!

The "highlightProblem" function calls the "findText" function and makes its code run.

As a result, the program performs the same searching and highlighting as before.

To search for different words, call the “findText” function again, passing in different search results each time.

Pass in “very” as the parameter.

Then, type “findText” again and pass in “totally” as the parameter.

Now your program will search for both “very” and “totally” in your document.

Add a variable called "item" to the “findText” parameter.

At the top of the script editor, type “item” in the parentheses beside “findText.”

To see if the code is working properly, add a logger entry that shows the contents of the “item” variable.

Test it out by running the function "highlightProblem."

Check the log.

Great!

The code worked.

"HighlightProblem" calls "findText" with the parameter "very."

"FindText" puts the word "very" into the log, then logs the result "rangeElement."

Next, the word "totally" is passed in as a parameter and logged the same way.

Add comments to help you remember what each line does.

Though the parameters "very" and "totally" are saved to the log, only the word "very" is highlighted in the document.

The word "very" is still used in the code for “findText.”

Change these parameters to the “item” variable you created in the last step.

Since "item" is a variable, it stores any value passed to it.

The program uses the value to search and highlight the document.

Make sure you do not put quotes around the word “item.”

Variables do not use quotes.

Next, update your comment to explain how the code you just updated works.

Try it out.

Select all the text in the document and remove the highlighting.

Then, run your code again.

Great!

The words "very" AND "totally" are highlighted in the document.

You could review each highlighted item and choose better words to replace them.

Now, it's your turn.

Change the name of "myFunction" to "findText."

Then, add a new function called "highlightProblem."

Call "findText" from the “highlightProblem” function, passing in one parameter for "very" and one for "totally."

Change the "findText" function to receive a parameter called "item."

Log the value of "item" for testing.

Replace the word "very" with the new parameter "item" in the code Add and update comments in your program as you go.

Next arrow_forward
Instructions
  1. Rename "myFunction" to "find Text" and add parameters.
  2. Add function "highlightProblem" and call "findText" twice.
  3. Log the parameter.
  4. Replace items.
  5. Update the comments.
  6. Test it out!