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 trialemhes
6,934 PointsWhy all the functions?
As practice, I've been trying to write out the code on my own before Dave explains it. For this lesson I wrote something a bit different then what Dave suggests, but was able to accomplish the same thing:
var playList = [ 'I Did It My Way', 'Respect', 'Imagine', 'Born to Run', 'Louie Louie', 'Maybellene' ];
var maxLoop = playList.length var currentSong = ' ';
for (var i = 0; i < maxLoop; i += 1) { currentSong += ("<li>" + playList[i] + "</li>"); }
document.write("<ol>" + currentSong + "</ol>");
My question: Is Dave beginning to define functions, even if functions aren't needed, to get us in the practice for when we're working on bigger projects?
1 Answer
Ryan Field
Courses Plus Student 21,242 PointsBasically, yes. Having functions makes your code so much easier to manage and make modifications when they are necessary. It also makes it modular, which means you can port it and reuse it in other places when you need it.
Obviously, if you just want one thing to happen on a simple page, it doesn't really matter if you make functions or not, but it is a good practice to get into if you want to work on larger projects.