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
Jason Ladieu
10,635 PointsWhy use placeholders like 'songs' in the first place? Isn't it much easier to use the name of the variable?
var playList = [
['I Did It My Way', 'Frank Sinatra'],
['Respect', 'Aretha Franklin'],
['Imagin', 'John Lennon'],
['Born to Run', 'Bruce Springsteen'],
['Louie Louie', 'The Kingsmen'],
['Mabellene', '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] + '</li>';
}
listHTML += '</ol>';
print(listHTML);
}
printSongs(playList);
1 Answer
Casey Ydenberg
15,622 PointsHere, songs exists only within the printSongs function. This makes is really clear that songs is self-contained within that function and could be safely reused on arrays other than playList.
Using the same name within the function as outside of it could be an example of shadowing, which can be confusing. Not defining a songs variable and relying on closure is even worse, since the function is not reusable.
It should be said that these things aren't really an issue in code this simple. But they are examples of good practice that becomes more important in larger scripts/apps.
Jason Ladieu
10,635 PointsJason Ladieu
10,635 PointsOK, that makes a bit more sense. Is there a video on Treehouse that goes over this? I may have watched it already, but I could obviously do with a refresher.
Thanks,
Jason