Skip to content

Commit ac913f2

Browse files
fixed content changes in exercises and completed code
1 parent b0dbb47 commit ac913f2

2 files changed

Lines changed: 51 additions & 65 deletions

File tree

src/sections/06-linked-lists/02-linked-lists-intro/solution.js

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
// Linked Lists Introduction - Playlist Management System
2-
// Complete solutions for the playlist management functions
3-
1+
// 🎵 Simple Song Node class - represents a song in our playlist
42
class 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() {
12-
return `${this.title} - ${this.artist}`;
10+
return \`\${this.title} - \${this.artist}\`;
1311
}
1412
}
1513

@@ -49,7 +47,7 @@ function playNextSong(playlist, targetSong) {
4947
function removeSong(playlist, targetTitle) {
5048
// Handle case where first song should be removed
5149
if (playlist && playlist.title === targetTitle) {
52-
console.log(`🗑️ Removed first song: ${playlist.toString()}`);
50+
console.log(\`🗑️ Removed first song: \${playlist.toString()}\`);
5351
return playlist.next;
5452
}
5553

@@ -59,13 +57,13 @@ function removeSong(playlist, targetTitle) {
5957
if (currentSong.next.title === targetTitle) {
6058
const removedSong = currentSong.next;
6159
currentSong.next = removedSong.next;
62-
console.log(`🗑️ Removed song: ${removedSong.toString()}`);
60+
console.log(\`🗑️ Removed song: \${removedSong.toString()}\`);
6361
return playlist;
6462
}
6563
currentSong = currentSong.next;
6664
}
6765

68-
console.log(`🎵 Song "${targetTitle}" not found in playlist`);
66+
console.log(\`🎵 Song "\${targetTitle}" not found in playlist\`);
6967
return playlist;
7068
}
7169

@@ -78,25 +76,6 @@ function countSongs(playlist) {
7876
currentSong = currentSong.next;
7977
}
8078

81-
console.log(`📊 Total songs in playlist: ${count}`);
79+
console.log(\`📊 Total songs in playlist: \${count}\`);
8280
return count;
83-
}
84-
85-
// Test the functions
86-
console.log("=== Testing playNextSong ===");
87-
console.log(playNextSong(song1, "Hotel California - Eagles"));
88-
console.log(playNextSong(song1, "Sweet Child O' Mine - Guns N' Roses"));
89-
console.log(playNextSong(song1, "Nonexistent Song - Unknown"));
90-
91-
console.log("\n=== Testing removeSong ===");
92-
let modifiedPlaylist = removeSong(song1, "Hotel California");
93-
console.log("After removing Hotel California:");
94-
let current = modifiedPlaylist;
95-
while (current) {
96-
console.log(current.toString());
97-
current = current.next;
98-
}
99-
100-
console.log("\n=== Testing countSongs ===");
101-
console.log("Total songs:", countSongs(song1));
102-
console.log("Empty playlist count:", countSongs(null));
81+
}
Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
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
42
class 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;
2523
song3.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
/*
3130
function 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
/*
4048
function 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

Comments
 (0)