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

Syed Abbas
Syed Abbas
4,762 Points

Error-'undefined'

Hi I have followed the video in JavaScript object about building playlist application. When I run the program I get the error saying that htmlSring is undefined, even though it has been defined. Please find the code below:

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 === songs.length) { this.nowPlayingIndex = 0; } this.play(); };

function Song(title, duration, artist) { this.title = title; this.duration = duration; this.artist = artist; this.isPlaying = false; } Song.prototype.play = function () { this.isPlaying = true; }; Song.prototype.stop = function () { this.isPlaying = false; }; Playlist.prototype.renderInElement = function (list) { list.innerHTML = ""; for (var i = 0; i < this.songs.length; i++) {

    var string = this.songs[i].toHTML();
    list.innerHTML += string;

}

};

Song.prototype.toHTML = function () {

var htmlSrtring = '<li';
if (this.isPlaying) {
   htmlSrtring += 'class = "current"';
}
    htmlSrtring += '>';
    htmlSrtring += this.title;
    htmlSrtring += '-';
    htmlSrtring += this.artist;
    htmlSrtring += '<span class="duration">';
    htmlSrtring += this.duration;
    htmlSrtring += '</span></li>';
    '''return htmlString;'''

};

var mySong1 = new Song("tum mere ho", "akhtar", "2:32"); var mySong2 = new Song("jab hum jawan hon gay", "aslam", "4:32"); var playlist = new Playlist(); playlist.add(mySong1); playlist.add(mySong2); var playListElement = document.getElementById("playlist"); playlist.renderInElement(playListElement);

2 Answers

Syed Abbas
Syed Abbas
4,762 Points

OK. Thank you very much. I apologise for not previewing the code.

Stephan Olsen
Stephan Olsen
6,650 Points

No harm done :) It's more for your own sake, as it will be easier to get replies, the better the post is.

Stephan Olsen
Stephan Olsen
6,650 Points

In the future, please preview your post before submitting it. It can be really hard to decipher your code when it's not properly formatted. In this case it seems like you mispelled your variable.

// This is your variable definition.
var htmlSrtring = '<li'; 

// This is what you return
return htmlString;