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 trial

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Using For Loops with Arrays

Jonathan Martin
Jonathan Martin
Courses Plus Student 1,942 Points

2 Questions : #1 Why do you need another function to generate output of the "listHTML" and #2, ....

2 Questions :

1 Why do you need another function to generate output of the "listHTML"

this is the confusing part.

2 What particular library should I learn about combining JavaScript and HTML?

This lesson is great but as a beginner like me, it is "VERY" overwhelming to see codes like this lesson. .. generating HTML + JavaScript


1 Answer

var playList = [
  'I Did It My Way',
  'Born to Run',
  'Louie Louie',

  var listHTML = '<ol>';
  for ( var i = 0; i < playList.length; i += 1) {
    listHTML += '<li>' +playList[i] + '</li>';
  listHTML += '</ol>';
  1. You could have done it like this, without declaring any functions and there is nothing wrong with doing it this way. Some good reasons to put your code in functions is readability, debugging, variable scope, and your future self will thank you for it. Another important reason for functions is they're versatile, you can use them as many times as you'd like with different inputs.

  2. I would strongly recommend to keep working through some of the basic javascript, css, and html courses on here before jumping into a library such as jQuery. JQuery is a really fun library to work with but doing things that hard way first makes for a better understanding and experience once you do decide to jump into a library.

Here are some things that I use on a daily basis and have helped me become a better developer. https://developer.mozilla.org/en-US/ - documentation on all things html, css, & javascript http://eloquentjavascript.net/ - this book is packed full of information!