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
Kevin McNamara
7,483 PointsSongs not being rendered
I have been looking at my code for hours and still can not figure out why the songs aren't even appearing.
Here is my songs.js file:
function Song(title, artist, duration) { this.title = title; this.artist = artist; this.duration = duration; this.isPlaying = false; }
Song.prototype.play = function() { this.isPlaying = true; };
Song.prototype.stop = function() { this.isPlaying = false; };
Song.prototype.toHTML = function() { //back to UI later var htmlString = '<li'; if(this.isPlaying === true){ var htmlString += ' class="current"'; } var htmlString += '>'; var htmlString += this.title; var htmlString += ' - '; var htmlString += this.artist var htmlString += '<span class="duration">'; var htmlString += this.duration; var htmlString += '</span></li>';
return htmlString;
};
Here is playlist.js:
function Playlist() { this.songs = []; this.nowPlayingIndex = 0;
}
Playlist.prototype.add = function(song) { this.songs.push(song); };
Playlist.prototype.play = function() { var currentSong = this.songs[this.nowPlayingIndex]; currentSong.play(); };
Playlist.prototype.stop = function(){ var currentSong = this.songs[this.nowPlayingIndex]; currentSong.stop(); };
Playlist.prototype.next = function() { this.stop(); this.nowPlayingIndex ++; if(this.nowPlayingIndex === this.songs.length) { this.nowPlayingIndex = 0; } this.play(); };
Playlist.prototype.renderInElement = function(list) { list.innerHTML = ""; for(var i = 0; i < this.songs.length; i++) { list.innerHTML += this.songs[i].toHTML(); } };
And here is apps.js:
var playList = new Playlist();
var hesGone = new Song("He's Gone", "the Grateful Dead", "4:45"); var cassidy = new Song("Cassidy", "Big Dill and the Boys", "4:10");
playList.add(hesGone); playList.add(cassidy);
var playlistElement = document.getElementById("playlist"); playList.renderInElement(playlistElement);
var playButton = document.getElementById("play"); playButton.onclick = function() { playList.play(); playList.renderInElement(playlistElement); }
var nextButton = document.getElementById("next"); nextButton.onclick = function() { playList.next(); playList.renderInElement(playlistElement); }
var stopButton = document.getElementById("stop"); stopButton.onclick = function() { playlist.stop(); playList.renderInElement(playlistElement); }
Kevin McNamara
7,483 PointsSorry about that. Here is a link to my snapshot
1 Answer
rydavim
18,814 PointsYou're declaring the htmlString variable repeatedly when trying to add to it. When using +=, you're modifying the variable, not declaring it so don't use the var keyword.
Song.prototype.toHTML = function() {
//back to UI later
var htmlString = '<li'; // No 'var's after this!
if(this.isPlaying === true){
htmlString += ' class="current"';
}
htmlString += '>';
htmlString += this.title;
htmlString += ' - ';
htmlString += this.artist; // Added ;
htmlString += '<span class="duration">';
htmlString += this.duration;
htmlString += '</span></li>';
return htmlString;
};
That should do it, but let me know if you have any questions. Happy coding! :)
Steven Parker
243,656 PointsSteven Parker
243,656 PointsIt would help if you blockquote your code and include the HTML component. Even better, make a snapshot of your workspace and post the link to it here.