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!

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

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.