Skip to content

Instantly share code, notes, and snippets.

@stormsweeper
Created February 24, 2015 03:41
Show Gist options
  • Save stormsweeper/775c912c9f1a151d1203 to your computer and use it in GitHub Desktop.
Save stormsweeper/775c912c9f1a151d1203 to your computer and use it in GitHub Desktop.
My playlist // source http://jsbin.com/fiyelewuxo
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>My playlist</title>
</head>
<body>
<button onclick="addToPlaylist()">Add to playlist</button>
<h1>My playlist</h1>
<ol id="playlistElement"></ol>
<script id="jsbin-javascript">
/** Starting code:
var playlist = [];
function addToPlaylist() {
// 1. ask for a song
// 2. add it to the playlist array
// 3. call displayPlaylist
}
function displayPlaylist() {
// 1. get the playlistElement from the document
// 2. remove any child elements in it
// 3. add a child element (<li>) for each song
}
**/
var playlist = [];
function addToPlaylist() {
// 1. ask for a song
var song = prompt("What song do you want to add?");
// 2. add it to the playlist array
if (song) {
playlist.push(song);
}
// 3. call displayPlaylist
displayPlaylist();
}
function displayPlaylist() {
// 1. get the playlistElement from the document
var playlistElement = document.getElementById("playlistElement");
// 2. remove any child elements in it
while (playlistElement.firstChild) {
playlistElement.removeChild(playlistElement.firstChild);
}
// 3. add a child element (<li>) for each song
var i = 0;
while (i < playlist.length) {
var songElement = document.createElement("LI");
songElement.innerHTML = playlist[i];
playlistElement.appendChild(songElement);
i++;
}
}
/** Extensions:
1. Have the playlist always be sorted alphabetically
2. Don't add songs to the playlist if they are already there.
**/
</script>
<script id="jsbin-source-javascript" type="text/javascript">/** Starting code:
var playlist = [];
function addToPlaylist() {
// 1. ask for a song
// 2. add it to the playlist array
// 3. call displayPlaylist
}
function displayPlaylist() {
// 1. get the playlistElement from the document
// 2. remove any child elements in it
// 3. add a child element (<li>) for each song
}
**/
var playlist = [];
function addToPlaylist() {
// 1. ask for a song
var song = prompt("What song do you want to add?");
// 2. add it to the playlist array
if (song) {
playlist.push(song);
}
// 3. call displayPlaylist
displayPlaylist();
}
function displayPlaylist() {
// 1. get the playlistElement from the document
var playlistElement = document.getElementById("playlistElement");
// 2. remove any child elements in it
while (playlistElement.firstChild) {
playlistElement.removeChild(playlistElement.firstChild);
}
// 3. add a child element (<li>) for each song
var i = 0;
while (i < playlist.length) {
var songElement = document.createElement("LI");
songElement.innerHTML = playlist[i];
playlistElement.appendChild(songElement);
i++;
}
}
/** Extensions:
1. Have the playlist always be sorted alphabetically
2. Don't add songs to the playlist if they are already there.
**/
</script></body>
</html>
/** Starting code:
var playlist = [];
function addToPlaylist() {
// 1. ask for a song
// 2. add it to the playlist array
// 3. call displayPlaylist
}
function displayPlaylist() {
// 1. get the playlistElement from the document
// 2. remove any child elements in it
// 3. add a child element (<li>) for each song
}
**/
var playlist = [];
function addToPlaylist() {
// 1. ask for a song
var song = prompt("What song do you want to add?");
// 2. add it to the playlist array
if (song) {
playlist.push(song);
}
// 3. call displayPlaylist
displayPlaylist();
}
function displayPlaylist() {
// 1. get the playlistElement from the document
var playlistElement = document.getElementById("playlistElement");
// 2. remove any child elements in it
while (playlistElement.firstChild) {
playlistElement.removeChild(playlistElement.firstChild);
}
// 3. add a child element (<li>) for each song
var i = 0;
while (i < playlist.length) {
var songElement = document.createElement("LI");
songElement.innerHTML = playlist[i];
playlistElement.appendChild(songElement);
i++;
}
}
/** Extensions:
1. Have the playlist always be sorted alphabetically
2. Don't add songs to the playlist if they are already there.
**/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment