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

Daniel Hildreth
Daniel Hildreth
16,170 Points

Need Help Using Arrays

I need help with one of the code challenges using Arrays. It asks me "Use the array method that combines all of the items in an array into a single string. In the final string, the array items should be separated by a comma AND a space. Finally, log the final string value to the console." I have my code attached to this, but not quite sure how to log it into the console. I've tried:

console.log(print); console.log(print (month.join(', ');); console.log(month); console.log(join); console.log(months.join);

Yet with all those tries, I still get it wrong. Can someone help me please?

script.js
var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
print (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>
print (months.join(', ') ); // wrong

If print is your variable you should make it this way.

print = months.join(', ');
console.log( print );

It's not good variable name sometime because it sound like method. I will explain it below but you should check reserved words in js. Anyway, print is not reserved keyword by js so you can use it and redefine. BUt it's default method of Global Window object in your browser.

"Dot join()" notation is the method of Array object. Array object is some kind "forefather" for the array and string variables who have own method as .join(), .replace(), .match() and so on. Which you can access via "dot notation": somevalue.join() or somevalue.replace() and so on.

Arrays and String are not changeable so every method will not change original variable value and you need to assign result of it to the new var or redefine existence one or return it manualy to desired function.

months = months.join(', '); // or
var new = months.join(', '); // or
print( months.join(', ') ); // print method have no parameters at all
console.log( months.join(', ') ); // it will return result of months joining to the console

In your case you try to use global function/method "print". But methods and functions should be written without space between method name and parenthesis: "print()". There is some methods which do not need parenthesis at all:

months.length // method ".length" will return months array length
console.log( months.length ); // "console" is object which have log() method to return data to the console

But it's another case. Print method in my Chrome is reserved to print page

print( months.join(', ') ); // print is the Global method of 

Otherwise it will be just a link to the Global "print" function.

I'm not so good teacher and still learning basics of js but it information you can find in MDN or w3school.

2 Answers

month.join(', ') return array joined to the string You can make it this way:

months = month.join(', ');
console.log(month);

or

console.log( month.join(', ') );
Darren Joy
Darren Joy
19,573 Points

You are trying to console.log the 'print' variable but you don't seem to define it in the code... print needs to have var in front of it, and then be 'equal' to something

i.e. Are you trying to do print = month.join... etc?