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 Object-Oriented JavaScript (2015) Constructor Functions and Prototypes Making the UI Work

Rolando Arroyos
Rolando Arroyos
8,392 Points

undefinedundefined error on UI

I am getting "undefinedundefined" when i try to preview my app. Not sure what is going on. i followed all the steps.

app.js

var playlist = new Playlist();

var hereComesTheSun = new Song("Here comes the sun", "The Beatles", "2:54"); var walkingOmnsunshine = new Song("Walking On sunshine", "Katrina and the Waves", "3:43");

playlist.add(hereComesTheSun); playlist.add(walkingOmnsunshine);

var playlistElement = document.getElementById("playlist"); console.log(playlistElement); 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);

}

playlist

function Playlist() {

this.songs = []; this.nowPlayingIndex = 0; }

Playlist.prototype.add = function() { this.songs.push(Song); };

Playlist.prototype.play = function() { var currentSong = this.songs[this.nowPlayingIndex]; };

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 = ""; console.log(list); for (var i = 0; i < this.songs.length; i++) { // console.log(this.songs[i].toHTML); list.innerHTML += this.songs[i].toHTML;

} };

song.js

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() { var htmlString = '<li '; if(this.isPlaying){ htmlString += 'class="current"'; } htmlString += '>'; htmlString += this.title; htmlString == ' - '; htmlString += this.artist; htmlString += '<span class="duration">' htmlString == this.duration; htmlString += '</span></li>'; console.log(htmlString); return htmlString; };

3 Answers

Steven Parker
Steven Parker
229,644 Points

I saw four code errors in playlist.js:

  • on line 7 the function is missing an argument ("Song" would match the code in the body)
  • between lines 12 and 13 there should be a line with "currentSong.play();"
  • on line 17 a pair of parentheses are missing after "stop" to invoke the method
  • on line 36 another pair of parentheses are missing after "toHTML" to invoke the method
Rolando Arroyos
Rolando Arroyos
8,392 Points

thanks, this fixed my issue.

Steven Parker
Steven Parker
229,644 Points

Without code formatting, it's hard to see where lines start and end, but it looks like there might be a couple of syntax errors on this "line":

Song.prototype.toHTML = function() { var htmlString = '<li '; if(this.isPlaying){ htmlString += 'class="current"'; } htmlString += '>'; htmlString += this.title; htmlString == ' - '; htmlString += this.artist; htmlString += '<span class="duration">' htmlString == this.duration; htmlString += '</span></li>'; console.log(htmlString); return htmlString; };

It looks like you're assembling a string in pieces, but there are a couple of comparison operators ("==") where it seems there should be a concatenating assignment operator ("+=").


For future questions (or to fix this one), use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

And a good way to share an entire project in a way that makes it easy to analyze is to make a snapshot of your workspace and post the link to it here.

Rolando Arroyos
Rolando Arroyos
8,392 Points

I created a snapshot, since i had those syntax errors on that htmlString. Here it is: https://w.trhou.se/mcmg2h7gks

Rolando Arroyos
Rolando Arroyos
8,392 Points

i figured out the issues with the prior snapshot, the only aspects not working is the stlying. it seems li.current is not changing. not sure if it is a real issue or just me needing to restart my browsers. anyway, here is the new snapshot:

https://w.trhou.se/mcmg2h7gks

Steven Parker
Steven Parker
229,644 Points

Unless you changed the previous one also, this is the same snapshot.