Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Basics Perfecting the Prototype Censoring Words - Looping Until the Value Passes

2 Answers

Okay, so a boolean is a data type just like String or Int, but a boolean has either a value of true or false.

In the first part of the video Craig is running a loop that will do something as long a certain condition evaluates to true:

do {
    // Ignoring the code here
} while (noun.equalsIgnoreCase("dork") || noun.equalsIgnoreCase("jerk"))

As long as the string that is stored in the variable called noun is either "dork" or "jerk" the loop will continue to run.

Later in the video he extracts out this expression:

noun.equalsIgnoreCase("dork") || noun.equalsIgnoreCase("jerk")

into a boolean variable called isInvalidWord which will store either true or false depending on whether or not the variable noun equals either "dork" or "jerk". He does this because he originally had that expression:

noun.equalsIgnoreCase("dork") || noun.equalsIgnoreCase("jerk")

twice in his code (at the 4:00 mark in the video it is on lines 26/27 as well as line 30) and duplicating the same code multiple times is bad practice so extracting it into its own variable solves this duplication.