Welcome to the Treehouse Community
Looking to learn something new?
Andy tran7,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!!
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.