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 Using For Loops with Arrays

Jonathan Martin
PLUS
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

Thanks

1 Answer

var playList = [
  'I Did It My Way',
  'Respect',
  'Imagine',
  'Born to Run',
  'Louie Louie',
  'Maybellene'
];

  var listHTML = '<ol>';
  for ( var i = 0; i < playList.length; i += 1) {
    listHTML += '<li>' +playList[i] + '</li>';
  }
  listHTML += '</ol>';
  document.write(listHTML);
  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!