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 trialjiwan gurung
4,248 Pointswhat am i doing wrong?
does the join method need to be inside the if statement?
var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var singleString = months.join(', ');
for (i =0; i<months.length; i++){
console.log(singleString[i]);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JavaScript Loops</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
3 Answers
Dan Weru
47,649 PointsHello, you're doing great. You don't need the for loop though; the join function is enough on its own (does all the heavy lifting for you).
var singleString = months.join(', ');
// then log the singleString to the console
In fact, you really don't have to save the string in a new variable. For instance you could do
console.log(months.join(', ')); // log the new string directly to the console
Dave StSomeWhere
19,870 PointsYou don't have an if statement anywhere.
Also, the challenge is asking for you to just use the .join() function.
It doesn't make sense to use a loop on singlestring since the .join() function returns a string, so it doesn't have any index to iterate. The .join() function does the looping for you to put the comma space between each array element.
jiwan gurung
4,248 Pointsthanks guys
Dan Weru
47,649 PointsYou're most welcome