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 Array Methods

Jacob Tennyson
Jacob Tennyson
6,240 Points

Array methods???

im confused..

script.js
var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];

var search;

function print(message){
  document.write('<p>' +message+ '</p>'); 
  print.months(months.join(', '));
}

console.log(print);
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

1 Answer

Grace Kelly
Grace Kelly
33,990 Points

Hi Jacob you are using the correct array method (months.join(', ') ) and you have the right idea however this can be made much more simply. For example, we don't need a print function as all we are doing is logging the information to the console. We can break this simple process down into the following two steps:

  1. Create a variable called year that joins all the values of the months array separated by ', '
  2. Log the year variable to the console

By using these steps we can do the following:

var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var year = months.join(', '); //create year variable
console.log(year); //log the year variable to the console

Hope that helps!!