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 trialramseydennis
4,530 PointsHow is "songs" influencing the var playList?
Hey guys,
How does this:
function printSongs(songs) {
var listHTML = '<ol>';
for (var i = 0; i < songs.length; i += 1) {
listHTML += '<li>' + songs[i] + ' by ' + songs [i][1] + '<li>'>;
}
Influence this:
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']
];
I don't see the connective tissue, if you will -
Thanks in advance -
4 Answers
Dave McFarland
Treehouse TeacherHi ramseydennis
You've got it right that this is the important bit:
printSongs(playList);
The variable playList
holds an array of values. In other words, it stores DATA that we want to access in the function printSongs
. When you call the function with this line -- printSongs(playList)
-- you're passing the array of songs to the function printSongs()
. In the function, that array is then stored in a variable (called a parameter in this case) named songs
. That's what happens here
function printSongs( songs ) {
}
When the function runs, that songs
variable holds the array -- basically the data in the playList
variable. Hope that helps.
Raquel Smith
10,683 PointsI just want to make sure I'm understanding this correctly. So...
- Functions are meant to be reusable with different datasets (and therefore variables)
- To use them with different variables, you have to put a placeholder variable in the function so that the structure is correct
- When the function is called, it is passed an argument (AKA a variable) that will replace the placeholder
- The function runs, it uses the variable that was passed to it and all the information in that variable
- The placeholder variable doesn't do anything else in the program
Is that correct?
Ethan Weeks
15,352 PointsI am going to go out on a limb and say that printSongs function is being invoked with playlist as an argument. Something like this :
printSongs(playList);
//When that function gets called songs parameter will have the value of playlist
When defining a function the words you put in the parenthesis are just parameters, like placeholders.
since playList was the array being passed in when the function was invoked it now will be assigned where ever songs was.
ramseydennis
4,530 PointsHey Ethan,
Thanks for your response -
I don't understand how songs is operating as the variable playList - can you elaborate on this concept?
I don't see it, nor understand how the browser knows that songs is synonymous with playList - as I am interpreting you to claim.
Does it have to do with this line at the end? -
printSongs(playList);
If so, how? Why does this work?
Thanks again for your help -
Luis Diaz
5,617 PointsSongs is just a placeholder. If you use or call the function like so: printSongs(playList); Then the function is executed with the variable "playList", and the array or information it contains, replacing songs in the code of the function.
Vlad Legkowski
9,882 PointsVlad Legkowski
9,882 PointsI have same issue mate, dont get it! You figured it out?