Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

My songs are not showing up in the playlist and the browser is saying undefinedundefined

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();
  }
};

app.js

var playlist = new Playlist();

// just adding the path parameter we defined in our object - you'll need to upload the files.
// I put them in an audio folder, but you don't have to. You'd just leave off the audio/ if not.
var hereComesTheSun = new Song("Here Comes the Sun", "The Beatles", "2:54", "audio/hereComesTheSun.mp3");
var walkingOnSunshine = new Song("Walking on Sunshine", "Katrina and the Waves", "3:43", "audio/walkingOnSunshine.mp3");

playlist.add(hereComesTheSun);
playlist.add(walkingOnSunshine);

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);
}

song.js

function Song(title, artist, duration) { // added the path parameter
  this.title = title;
  this.artist = artist;
  this.duration = duration;
  this.isPlaying = false;
  this.mp3 = new Audio(path); // created the mp3 property which is an HTMLAudioElement
}

Song.prototype.play = function() {
  this.mp3.play(); // added this to actually play the audio
  this.isPlaying = true;
};

Song.prototype.stop = function() {
  this.mp3.pause(); // HTMLAudioElement doesn't have a stop() method,
  this.mp3.currentTime = 0; // so we pause and reset the time instead.
  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>';
  return htmlString;
};

2 Answers

Steven Parker
Steven Parker
220,696 Points

In function Song (song.js) you reference a variable path that doesn't exist in that context. There's even a comment on the first line that says "added the path parameter", but there is no path parameter!

But if you DO add it, your songs will show up.

function Song(title, artist, duration, path) { // added the path parameter
  this.title = title;
  this.artist = artist;
  this.duration = duration;
  this.isPlaying = false;
  this.mp3 = new Audio(path); // created the mp3 property which is an HTMLAudioElement
}

the songs are still not showing up and there's no errors in the console.