Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.