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
Richard Nash
24,862 PointsTreeTunes Issue... Buttons not working properly...
Hi everybody. I followed along with Andrew Chalkley through the object Oriented JavaScript course but My final code for the TreeTunes music player is not working correctly, specifically the buttons. Play does not play, next seems to be ok, stop does nothing, but play seems to stop. I;m only going off the color changing on the playlist songs...
Anyway, here is the app.js file:
var playlist = new Playlist();
var hereComesTheSun = new Song('Here comes the Sun', 'The Beatles', '2:54');
var walkingOnSunshine = new Song('Walking on Sunshine', 'Katrina and the Waves', '3:43');
var stardust = new Song('Stardust', 'RYKR', '4:53');
var manOfSteel = new Movie('Man of Steel', 2013, '2:23:00');
playlist.add(hereComesTheSun);
playlist.add(walkingOnSunshine);
playlist.add(stardust);
playlist.add(manOfSteel);
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');
playButton.onclick = function()
{
playlist.stop();
playlist.renderInElement(playlistElement);
}
Here is the media.js file:
function Media(title, duration)
{
this.title = title;
this.duration = duration;
this.isPlaying = false;
};
Media.prototype.play = function()
{
this.isPlaying = true;
};
Media.prototype.stop = function()
{
this.isPlaying = false;
};
Here is the song.js file:
function Song(title, artist, duration)
{
Media.call(this, title, duration);
this.artist = artist;
};
Song.prototype = Object.create(Media.prototype);
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;
};
Here is the movie.js file:
function Movie(title, year, duration)
{
Media.call(this, title, duration);
this.year = year;
};
Movie.prototype = Object.create(Media.prototype);
Movie.prototype.toHTML = function()
{
var htmlString = '<li';
if (this.isPlaying)
{
htmlString += ' class="current"';
}
htmlString += '>';
htmlString += this.title;
htmlString += ' - ';
htmlString += this.year;
htmlString += '<span class="duration">'
htmlString += this.duration;
htmlString += '</span></li>';
return htmlString;
};
Here is the playlist.js file:
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 the index.html file, for reference:
<!DOCTYPE html>
<html>
<head>
<title>Treetunes</title>
<link rel="stylesheet" href="style.css"/>
</head>
<body>
<div>
<h1>Treetunes</h1>
<ol id="playlist">
</ol>
<button id="play">Play</button>
<button id="next">Next</button>
<button id="stop">Stop</button>
</div>
<script src="playlist.js"></script>
<script src="media.js"></script>
<script src="movie.js"></script>
<script src="song.js"></script>
<script src="app.js"></script>
</body>
</html>
I'm sure it's probably something stupid that I'm missing, but any help would be awesome, as the console shows no errors...
Thank you in advance :-)
Richard
Richard Nash
24,862 Pointssorry, did not know it was private. Will add the code to the original question :-)
Richard Nash
24,862 PointsAlso, was curious to get your thoughts on a topic. Here are a couple articles from a fella named Eric Elliot that has piqued my interest:
https://medium.com/javascript-scene/the-two-pillars-of-javascript-ee6f3281e7f3
He is advocating avoiding using classes or anything resembling classes as they become brittle and arthritic in large code bases. As I'm getting to the point in my JS learning where I'm starting to think about these things I'm starting to ask around and get different opinions on the subject.
Anyway, any help that you can share with me is super appreciated.
Thank you!
Richard
1 Answer
Justin O'Reilly
3,546 PointsHey there,
In your app.js file your stopButton function is currently playButton.onclick change that to stopButton.onclick and you should be good to go.
Currently:
var stopButton = document.getElementById('stop');
playButton.onclick = function()
{
playlist.stop();
playlist.renderInElement(playlistElement);
}
Should Be:
var stopButton = document.getElementById('stop');
stopButton.onclick = function()
{
playlist.stop();
playlist.renderInElement(playlistElement);
}
Richard Nash
24,862 Pointsle sigh... sooooo simple....yet so hard to spot... thank you Justin O'Reilly :-)
Andrew Chalkley
Treehouse Guest TeacherAndrew Chalkley
Treehouse Guest TeacherHi Richard Nash - you need to share a "Snapshot" of your workspace not your actual workspace. That belongs to you :)