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

Candace Cohen
Candace Cohen
Courses Plus Student 2,120 Points

Cannot read property 'add' of undefined

I keep getting an error in regards to the playlist.add but I am not sure what I am doing wrong? var playlist = Playlist ();

var giveItToYou = new Song ("Give it to you", "Robin Thicke", "3:50"); var fancy= new Song("Fancy", "Iggy Azalea", "3:20"); var wiggle= new Song("Wiggle", "Jason Derulo", "3:20");
var upDown= new Song("Up Down", "T-pain", "3:52"); var bringTheNoise= new Song("Bring the Noise", "M.I.A", "3:24"); var goGirl= new Song("Go Girl", "Ciara", "3:56");

playlist.add(giveItToYou); playlist.add(fancy); playlist.add(wiggle); playlist.add(upDown); playlist.add(bringTheNoise); playlist.add(goGirl);

var playlistElement = document.getElementById("playlist");


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

1 Answer

I think you need "new" before "Playlist()" near the top.

var playlist = new Playlist ();