4. Create New Conditional Statements

Playback Speed:
Transcript

In the previous video, you edited the talkbot so it could respond to new words.

In this video, you will add more conditional statements so your talkbot can respond to more than one thing.

To do this, you will copy the existing conditional statement and make changes to the strings.

Copy lines 10 through 15 exactly as shown.

Use the keyboard shortcut for copy: Control-C or Command-C.

Press Enter or Return to leave a break in your code, then paste the new conditional statement using the keyboard shortcut, Control-V or Command-V.

Make sure your code matches what’s on screen exactly.

Now, program the new string to respond to the words knock knock.

“Who’s there” is the new conditional statement that needs to be met.

The next line of this joke is “woo,” so add that as the response to “who’s there” in the new conditional statement.

Okay, save your code… Then reload the page to test it again.

And ask for a joke.

It didn’t work. Debug to find out why.

The word “who’s” has an apostrophe in it.

It’s a special character that can’t be included in the string, because it disrupts the code.

Notice how the other strings are all red, and this one turns from red to black.

To fix it, change your string to ”who is there,” and save your code.

Reload the app again, and try it again.

That worked!

But most people would probably just type “who’s there” with the apostrophe instead of “whos there” with no apostrophe.

Since it’s too hard to predict if someone will spell who’s correctly, change the string to make it shorter.

That way, the condition will be easier to meet.

Use the word “there,” since you’ll use the word “who” again later.

Make sure you spell it T-H-E-R-E, exactly as you see on screen.

Now a simple spelling or grammatical error won’t stop the joke.

Finally, notice that all of the input strings, like “joke” and “there,” are lowercase.

Remember, the computer sees a capital B and a lowercase b as two different things, just like it sees the words “who is” and “who’s” as different things.

But someone typing in the talkbot might use both lowercase and capital letters.

For the computer to recognize both, there’s a special line of code written in line nine that changes everything that’s typed in the talkbot to lowercase so that your conditional statement only has to look for lowercase strings.

Add a comment to remind you about what this code does.

Now, finish the joke.

In the script editor, copy and paste the conditional statement one more time.

In the third conditional statement, change the strings to “woo who”...

And finally the response: “Woo hoo! You wrote some code.”

Save your code and reload the app to test your talkbot’s joke again.

Funny, right?

Debug if you need to.

Make sure you’re typing what you see on screen exactly.

Now anyone who knows how to tell a knock-knock joke will be able to successfully use your talkbot.

To test it out, swap seats with a classmate and test each other’s talkbots.

Determine whether you can complete the entire joke cycle as the programmer intended.

Instructions

  1. Copy and paste the conditional statements two more times.
  2. Edit your code to look for a new string and respond with a new string for both conditional statements.
  3. Test your talkbot.
  4. Debug your code, if necessary.
  5. Swap seats with a classmate to test each other’s code.