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.

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 2

Andy tran
Andy tran
7,112 Points

Please help clarify why we need to create a function print(message) for a single command like document.write(message)?

In the video, Dave says we should create a function print(message) with a document.write(message) inside it because it will not affect the other codes that call that function. He also says if we have a script that calls that function in 20 different places-and when we need to improve our code, we just need to change the document.write(message) without going to 20 different places. Can someone please explain again the purpose for creating this function print(message)? Thanks!!

1 Answer

Think of it like this.

Say your currently message is Congrats! You got this answer right but you wanted it to be Congratulations! You got this answer right.

Calling a function that generates this requires you to up date it in one spot. If you just did document.write for each one and the quiz is five questions long. Its not a big deal right?

Now imagine this quiz is 500 questions long. You would have to do a find and replace and adjust it that way or go through and manually change them all. it also affects readable of the code.

Lot easier to understand that message returns you got this question wrong or you got this message right vs seeing if else for 500 questions.