Exercise: Road-Trip Playlist
Finish five functions that keep a road-trip playlist up to date, adding and removing songs, totaling the play time and checking for a song, and let the tests check each one.
Apprentice · JavaScript · Updated
Four friends are driving six hours to the lake, and the playlist won't sit still. Someone adds a song at every gas stop, the driver wants one moved to the front, a song that everyone's sick of has to go, and somebody keeps asking "how long is all this, anyway?". You'll write the code behind a small playlist app: five functions in playlist.js, each one using what this chapter taught. Tests tell you when each function works.
Get the exercise code
Open this lesson's GitHub repository, github.com/ask-codey/exercise-javascript-road-trip-playlist, also linked from the Exercise box beside the lesson. Clone it into the folder where you keep this course's work. In a terminal, in that folder:
git clone https://github.com/ask-codey/exercise-javascript-road-trip-playlist.git
Git makes a folder, exercise-javascript-road-trip-playlist, with the exercise code inside.
The folder holds:
playlist.js: your code. The five functions are already there, each returning a placeholder.trip.js: a small program that plans a trip with your functions and prints the result. You don't need to change it.playlist.test.js: the tests that check your functions. You don't need to change these.package.json: tells Node that the files useimportandexport.
Open the folder in VS Code, and open a terminal there with Terminal > New Terminal.
What to do
Every function is already declared, with its parameters, and returns a placeholder for now. The comment above it says what it should do, with examples:
/*
* Add the song to the END of the playlist, then return the playlist.
*
* addSong(['Highway Sunrise'], 'Desert Radio') -> ['Highway Sunrise', 'Desert Radio']
* addSong([], 'Windows Down') -> ['Windows Down']
*/
export function addSong(playlist, title) {
return playlist;
}
The word export in front of each function lets the tests and trip.js use it. Module 3 explains it properly, in Modules: import and export.
- Finish
addSongandplayNext. One adds a song to the end of the playlist, the other to the front, so it plays next. Both return the same playlist they were given. - Finish
isOnPlaylist. It gives backtrueorfalse. Titles match exactly, capital letters included. - Finish
totalPlayTime. It gets an array of song lengths in seconds, such as[214, 187, 243], and gives back their total. Use a loop. - Finish
removeSong. It removes the first song with that title. If the song isn't on the playlist, nothing changes.
Check your work
Run the tests from the exercise folder:
node --test
At first every test fails. That's expected: each function you finish turns its tests green. When a test fails, its message says which songs it used and what it expected, for example Removing Gas Station Coffee should leave the other three songs, in order.
To see your functions at work, run the trip planner:
node trip.js
Hints
- Work out an example by hand first. Write down
['Highway Sunrise', 'Desert Radio']and what it should look like after each function. - Ends of the list. Adding at the end and adding at the front each take one array method. Adding and Removing Items has both pairs.
- Removing takes two steps. First find where the song is, then remove the item at that place. Finding Things in an Array shows how the two methods fit together.
- The song that isn't there. Try removing a title that isn't on the list. What does your first step give back, and what does your second step do with it?
- The running total. Looping Through an Array shows the shape: a variable that starts at 0 before the loop, and grows inside it.
When you're done
Every test is green, and node trip.js prints:
Road-trip playlist:
1. Sunset Detour
2. Highway Sunrise
3. Desert Radio
4. Windows Down
5. Mile Marker 9
Desert Radio on the list: true
Total play time: 1147 seconds (19 min 7 s)
Now look back at removeSong. If a song were on the list three times, how would you change it to remove every copy?
Submit your exercise
The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here. You don't need Git for this: you upload your files in the browser, as the lesson Put Your Code on GitHub (Module 1) shows screen by screen.
- On GitHub, click +, then New repository. (No account yet? Sign up at github.com: it's free.) Name it (for example
exercise-javascript-road-trip-playlist), choose Public, turn Add README on so the upload button appears, and click Create repository. Your folder's own README replaces GitHub's when you upload it. - Choose Add file, then Upload files. In your exercise folder, select everything (Ctrl+A, or Cmd+A on a Mac) and drag it onto the upload area. Drag what's inside the folder, not the folder itself, so
package.jsonlands at the top of your repo. - Under Commit changes, type a short note, such as My solution, and choose Commit changes.
- Paste your repo's address, such as
https://github.com/your-name/exercise-javascript-road-trip-playlist, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.
Changed something after you submitted? Upload the changed files again (each one replaces the file with the same name), commit, and submit again: your latest submission is the one that counts.
So what
Adding, removing, totaling and checking: those four jobs are most of what any app does with a list, from a music player's queue to a store's shopping cart. Now you can write all four.