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

not sure on the final output

not sure what the challenge is looking for

script.js
var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
months.join
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

2 Answers

So this code challenge is asking you to replace the array in the months variable with a string using the join method. The string should include the months separated by a comma and a space, so we pass ', ' in as an argument of the join method. We then to print that string out to the console.

e.g:

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

It's probably not best practice to go replacing an whole array with a string, if this was a const variable, we'd get an error. Ideally, we would create a new variable when concatenating the months array. But hey, this is what the code challenge is asking for, so who am I to argue!

Hi Berian,

Thanks. I tried the update and now I get this error:

TypeError: 'undefined' is not a function (evaluating 'months.join(", ")')

What do you think?

thanks

Ahh so reading the task again, my initial thought on replacing the array was correct. It's not really clear in the instructions, but it actually wants you to create a new variable called final. Let's use const variables here to ensure that nothing is going to be replaced that we don't want to:

const months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
const final = months.join(', ');
console.log(final);