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

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 Points

My 'next' button doesn't work.

I've got myself muddled with this one. When I click my next button (or stop button), it doesn't remove the 'current' class on the previous item so it stays pink. It's probably something stupidly obvious.

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

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 manOfSteel = new Movie("Man of Steel", 2013, "2:23:00");

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

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");
stopButton.onclick = function() {
  playlist.stop();
  playlist.renderInElement(playlistElement);  
}

media.js

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 = stop;
};

song.js

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

movie.js

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 += ') ';
  htmlString += '<span class="duration">';
  htmlString += this.duration;
  htmlString += '</span></li>';
  return htmlString;
  console.log(htmlString);
};

style.css

@import url(http://fonts.googleapis.com/css?family=Varela+Round);
@import url(http://necolas.github.io/normalize.css/3.0.2/normalize.css);

body {
    background: #ECEEEF;
    text-align: center;
    margin: 100px auto;
    width: 80%;
    padding: 0 40px;
    font: 14px normal 'Varela Round', Helvetica, serif;
}

h1 {
    font-size: 20px;
    color:white;
}

div {
    background: #c25975;
    border-radius: 4px;
    border: 1px solid #D5DDE4;
    margin: -40px 0 0;
}

ol {
    list-style-type: none;
    padding: 0;
    background:white;
    margin: 0 0;
    width: 100%;
    display: inline-block;

}

li {
    color: #2C3238;
    width: 96%;
    padding: 15px 2%;
    margin: 0;
    text-align: left;
    border-top: 1px solid #D5DDE4;
}

li span {
    float: right;
    color: #777B7E;
}

li.current {
    background: #ffd7d8;
}

button {
    margin:10px;
    padding:10px 35px;
    border: none;
    border-radius: 5px;
    background: white;

}

index.html

<!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>

1 Answer

Hi Brendan,

It's indeed one small mistake and it's located in your media.js file. Where you wrote:

Media.prototype.stop = function() {
  this.isPlaying = stop;
};

It should have been:

Media.prototype.stop = function() {
  this.isPlaying = false;
};

Cheers,

Elian