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 Two-Dimensional Arrays

John Erickson
seal-mask
.a{fill-rule:evenodd;}techdegree
John Erickson
Full Stack JavaScript Techdegree Student 3,916 Points

Two-Dimensional Arrays

Good afternoon Treehouse Community,

So after my second viewing of this course, I think for the most part I understand what Dave's doing throughout this video lecture. I do however have a question.

As I'm new to javaScript I hope my question makes sense.

ls there a purpose in naming the parameters 'songs' for function 'printSongs' rather than just the argument 'playList'? Did Dave name it something different for flexibility purpose providing the option of making adjustments to the parameter songs without affecting the array 'playList'?

I guess what I'm asking is why not name the parameter the argument considering Dave is not adjusting the array 'playList'? I tested the code changing 'songs' to 'playList' and it still works fine and seems less confusing.

Although I'm getting a better understanding of javaScript concepts I'm having a difficult time putting it all together.

Does anyone know of a book, better yet site which is project/problem oriented? It's great understanding theory, however at this point I feel I'm not retaining the subject material as much as I'd like and believe a more hands on approach would be of benefit. Luckily I'm starting a web bootcamp come Monday.

Any help on this subject would be greatly appreciated.

Thanks, John

var playList = [ ['I Did It My Way', 'Frank Sinatra'], ['Respect', 'Aretha Franklin'], ['Imagine', 'John Lennon'], ['Born to Run', 'Bruce Springsteen'], ['Louie Louie', 'The Kingsmen'], ['Maybellene', 'Chuck Berry'] ];

function print(message) { document.write(message); }

function printSongs( songs ) { var listHTML = '<ol>'; for ( var i = 0; i < songs.length; i += 1) { listHTML += '<li>' + songs[i][0] + songs[i][0] + '</li>'; } listHTML += '</ol>'; print(listHTML); }

printSongs( playList );

Noob question, but how exactly do users post code using the Challenge format?

3 Answers

Steven Parker
Steven Parker
229,732 Points

Without seeing the actual code you changed I can only speculate, but it could be that your substitution works under some conditions, but there may be others (possibly obscure) where the substitution would operate differently than intended.

I do recall from this course that there a objects of type PlayList and others of type Song that have many similar properties, but they are not completely identical.

If you want some additional practice solving problems by coding, you might like Code Wars.

John Erickson
seal-mask
.a{fill-rule:evenodd;}techdegree
John Erickson
Full Stack JavaScript Techdegree Student 3,916 Points

Sorry about that Steven,

I applied the code in a edit to my original post. Question, do you happen to know how I can present code in a post similar to what other users do and what's seen in challenges?

As you can see Dave calls the function printSongs with a argument of 'playList'. The function printSongs has a parameter of songs. My question is why not for simplicity sake use 'playList' as both the argument and parameter instead of 'songs'? I'm certain you understand, however spelling it out helps me grasp the concepts or at least I think it does, LOL.

Steven Parker
Steven Parker
229,732 Points

Remember that the parameter names used in declaring functions are not important, though it is a good practice to use a name that indicates what kind of thing is expected as an argument. I can't speak for Dave, but I assume the function is intended to work on any object that contains a collection of Song items, and a PlayList is a specific example of the generic case.

The instructions for formatting code are found in the Markdown Cheatsheet pop-up found below the answer area :arrow_heading_down:

Steven Parker
Steven Parker
229,732 Points

Sorry if my terminology was imprecise. The name you use when calling a function is important. I was saying the name you use when defining the function is not, other than it must be used consistently within the definition.

So yes, the way you phrased it is correct. :ok_hand:

John Erickson
seal-mask
.a{fill-rule:evenodd;}techdegree
John Erickson
Full Stack JavaScript Techdegree Student 3,916 Points

Thanks Steven...Long story long being new to code/programming, let alone languages is very confusing and the terminology can cause a lot of confusion. I guess it doesn't help that I'm trying to learn javaScript, HTML, CSS, and Ruby at the same time; I start a web development bootcamp on Monday which they focus on Ruby. Speaking of which, out of curiosity are you a developer full-time? If so what's your take on javaScript vs. Ruby. I hear more and more people say to not learn Ruby and focus on javaScript, however know a couple friends of friends who work with Ruby on a daily basis. Anyway, thanks again for your help. Trust when I state I greatly appreciate it.

John Erickson
seal-mask
.a{fill-rule:evenodd;}techdegree
John Erickson
Full Stack JavaScript Techdegree Student 3,916 Points

Thanks again Steven, however wouldn't the function's argument name in this situation be important for it's for it's calling the array 'playList'?

Just so I'm not getting confused (which I am), a argument is what's listed inside the parenthesis when you're calling a function and a parameter is what's inside the parenthesis for the function being called, right?

So in the example listed above: printSongs( playList ) - 'playList is the argument function printSongs (songs) - 'songs' is the parameter