1- // Linked Lists Introduction - Playlist Management System
2- // Complete the functions below to work with a linked list playlist
3-
1+ // 🎵 Simple Song Node class - represents a song in our playlist
42class SongNode {
53 constructor ( title , artist ) {
64 this . title = title ;
75 this . artist = artist ;
8- this . next = null ; // Points to the next song
6+ this . next = null ; // Points to the next song in the playlist
97 }
108
119 toString ( ) {
@@ -25,51 +23,60 @@ song2.next = song3;
2523song3 . next = song4 ;
2624// song4.next is null (end of playlist)
2725
28- // Challenge 1: Find a song and play the next song in the playlist
29- // Uncomment and complete this function
26+ // ⏱️ Alex's First Playlist Challenge!
27+ // 🔓 Uncomment the below code section and implement the required logic:
28+
3029/*
3130function playNextSong(playlist, targetSong) {
3231 // Find the target song and play the song that comes after it
33- // Your code here
32+ // targetsong parameter is a string with "Tile - Band" ex. "Hotel California - Eagles"
33+ // Return the next song's string representation, or "End of playlist" if no next song
34+
35+ let currentSong = playlist;
36+
37+ // TODO: Traverse the playlist to find the target song
38+ // Hint: Use a while loop and check currentSong.toString()
39+
40+ return "End of playlist";
3441}
3542*/
3643
37- // Challenge 2: Remove a song from the playlist
38- // Uncomment and complete this function
44+ // ⏱️ Alex's Second Playlist Challenge!
45+ // 🔓 Uncomment the below code section and implement the required logic:
46+
3947/*
4048function removeSong(playlist, targetTitle) {
41- // Remove the song with the given title from the playlist
49+ // Remove a song from the playlist by title
4250 // Return the new head of the playlist
43- // Your code here
51+
52+ // Handle case where first song should be removed
53+ if (playlist && playlist.title === targetTitle) {
54+ return playlist.next;
55+ }
56+
57+ let currentSong = playlist;
58+
59+ // TODO: Find the song before the one to remove
60+ // Hint: Check if currentSong.next exists and if its title matches
61+
62+ return playlist; // Return original head if song not found
4463}
4564*/
4665
47- // Challenge 3: Count the total number of songs in the playlist
48- // Uncomment and complete this function
49- /*
50- function countSongs(playlist) {
51- // Count and return the total number of songs in the playlist
52- // Your code here
53- }
54- */
66+ // ⏱️ Alex's Third Playlist Challenge!
67+ // 🔓 Uncomment the below code section and implement the required logic:
5568
56- // Test your functions (uncomment to test)
5769/*
58- console.log("=== Testing playNextSong ===");
59- console.log(playNextSong(song1, "Hotel California - Eagles"));
60- console.log(playNextSong(song1, "Sweet Child O' Mine - Guns N' Roses"));
61- console.log(playNextSong(song1, "Nonexistent Song - Unknown"));
62-
63- console.log("\n=== Testing removeSong ===");
64- let modifiedPlaylist = removeSong(song1, "Hotel California");
65- console.log("After removing Hotel California:");
66- let current = modifiedPlaylist;
67- while (current) {
68- console.log(current.toString());
69- current = current.next;
70+ function countSongs(playlist) {
71+ // Count the total number of songs in the playlist
72+ // Return the count as a number
73+
74+ let count = 0;
75+ let currentSong = playlist;
76+
77+ // TODO: Traverse the entire playlist and count songs
78+ // Hint: Use a while loop and increment count for each song
79+
80+ return count;
7081}
71-
72- console.log("\n=== Testing countSongs ===");
73- console.log("Total songs:", countSongs(song1));
74- console.log("Empty playlist count:", countSongs(null));
7582*/
0 commit comments