Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# 🚀 Lab: Native Array Methods pt.2

This lab is an opportunity to enhance your JavaScript skills through hands-on practice with array methods `forEach`, `map`, `find`, `some/every`, and `sort()`.
Update README.md
This lab is an opportunity to enhance your JavaScript skills through hands-on practice with array methods `forEach`, `map`, `find`, `some/every`, and `sort()`.

## Getting Started

### 1. 🍴 Clone the Repository (from Github)

- On the GitHub page of this repository, click the green "Code" button.
- Copy the URL provided under "Clone with HTTPS".
- Open your terminal and execute:
Expand All @@ -16,6 +18,7 @@
### 1a. 🍴 Clone the Repository (without Github)

1. **Clone the Repository Directly:**

- Open your terminal on your local machine.

2. **Clone Using Terminal:**
Expand All @@ -25,8 +28,8 @@
```
- This command will clone the repository to your local machine.


### 2. 🔍 Explore the Problems

- In your terminal, navigate to the cloned repository's directory:
```
cd Lab-Native-Array-Methods-Pt2
Expand All @@ -38,25 +41,29 @@
- You will find the `index.js` file containing 20 problems, each using a different array method.

### 3. 💡 Work on the Problems

- Each problem in `index.js` focuses on practicing a specific array method.
- Read the instructions for each problem and try to solve it.
- Use `console.log()` within your solutions to output results.

### 4. 🏃 Run Your Code

- Test your solutions by running:
```
node index.js
```
- This command executes your code in `index.js`, allowing you to see the output in the terminal.

### 5. 🧪 Test Your Solutions

- To ensure your solutions are correct, use:
```
npm test
```
- This will run any predefined tests to validate your solutions.

### 6. 💾 Save Your Progress

- After solving each problem and verifying your solution, commit your progress:
```
git add .
Expand All @@ -65,13 +72,15 @@
- Replace `#` with the problem number and `Y` with the array method used.

### 7. 🔄 Repeat

- Continue working through each problem, running your code, testing, and committing your solutions.

### 8. 🚀 Next Steps

- After completing all the problems, you're ready for the next phase of your learning journey.
- In the upcoming lesson, you'll learn how to push your solutions back to GitHub, sharing your progress and receiving feedback.

### 9. 🆘 Need Help?

- If you find yourself stuck on a problem, don't hesitate to review the [MDN Web Docs on Array methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array).
- Collaboration is key to learning. Feel free to discuss approaches with your peers or reach out to a coach for guidance.

152 changes: 138 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
Native Array Methods pt.2 continues with the same dataset: songs. All required functions and array methods (forEach, map, find, some/every, sort) are combined into a single file, each addressing a distinct problem.
*/


const exampleSongData = require("./data/songs");
// Do not change the line above.


// #1
/**
* Returns the titles of songs sorted alphabetically.
Expand All @@ -15,16 +13,48 @@ const exampleSongData = require("./data/songs");
*/
function getSortedTitles(songs) {}




// #2
/**
* Returns the titles of all songs from a specified album.
* @param {Object[]} songs - An array of songs.
* @param {string} albumName - Name of the album.
* @returns {string[]} An array of song titles.
*/
function getSongsFromAlbum(songs, albumName) {}
function getSongsFromAlbum(songs, albumName) {
// const songsFromAlbum = [];

// for (const song of songs) {
// if (albumName === song.album) {
// songsFromAlbum.push(song.title);
// }
// }
// songs.forEach(song => {
// if (song.album === albumName) {
// songsFromAlbum.push(song.title);
// }
// })

return songs.reduce((songsFromAlbum, currSong) => {
if (currSong.album === albumName) {
songsFromAlbum.push(currSong.title);
}
return songsFromAlbum;
}, [])

// return songsFromAlbum;
}




// #3




// #3
/**
* Categorizes and counts songs based on their runtime.
* @param {Object[]} songs - An array of songs.
Expand All @@ -38,7 +68,21 @@ function categorizeSongsByRuntime(songs) {}
* @param {Object[]} songs - An array of songs.
* @returns {string} The name of the album with the most songs.
*/
function findAlbumWithMostSongs(songs) {}
function findAlbumWithMostSongs(songs) {
let highestSongCount = 0;
let albumWithMostSongs;

songs.reduce((songCountObj, currValue) => {
songCountObj[currValue.album] = (songCountObj[currValue.album] || 0) + 1;
if (songCountObj[currValue.album] > highestSongCount) {
highestSongCount = songCountObj[currValue.album];
albumWithMostSongs = currValue.album;
}
return songCountObj;
}, {});

return albumWithMostSongs;
}

// #5
/**
Expand Down Expand Up @@ -97,8 +141,21 @@ function getTotalRuntimeOfArtist(songs, artistName) {}
* Prints artists who have more than one song in the list.
* @param {Object[]} songs - An array of songs.
*/
function printArtistsWithMultipleSongs(songs) {}
function printArtistsWithMultipleSongs(songs) {
let artistSongCount = {};
songs.forEach((song) => {
if (artistSongCount[song.artist]) {
artistSongCount[song.artist]++;
} else {
artistSongCount[song.artist] = 1;
}
if (artistSongCount[song.artist] > 1) {
console.log(song.artist);
}
});
}

// console.log(printArtistsWithMultipleSongs(exampleSongData));
// Problem #12
/**
* Logs the longest song title.
Expand All @@ -120,7 +177,17 @@ function sortSongsByArtistAndTitle(songs) {}
* @param {Object[]} songs - An array of songs.
* @returns {Object} An object mapping each album to its total runtime.
*/
function listAlbumTotalRuntimes(songs) {}
function listAlbumTotalRuntimes(songs) {
let albumTotalRuntimes = {};
songs.forEach((song) => {
if (!albumTotalRuntimes[song.album]) {
albumTotalRuntimes[song.album] = song.runtimeInSeconds;
} else {
albumTotalRuntimes[song.album] += song.runtimeInSeconds;
}
});
return albumTotalRuntimes;
}

// Problem #15
/**
Expand All @@ -145,21 +212,79 @@ function mapArtistsToSongs(songs) {}
* @param {Object[]} songs - An array of songs.
* @returns {string} The name of the album with the longest average song runtime.
*/
function findAlbumWithLongestAverageRuntime(songs) {}

function findAlbumWithLongestAverageRuntime(songs) {
const albumAverageRuntime = {};
let albumName = "";
let longestAvgRuntime = 0;

songs.forEach((song) => {
// Create and update your albumAverageRuntime object - Gather your data***
if (!albumAverageRuntime[song.album]) {
// Create key values
albumAverageRuntime[song.album] = {
totalRuntime: song.runtimeInSeconds,
songCount: 1,
avgRuntime: song.runtimeInSeconds,
};
} else {
// Update object
albumAverageRuntime[song.album].totalRuntime += song.runtimeInSeconds;
albumAverageRuntime[song.album].songCount++;
// Get the average from our object's values
albumAverageRuntime[song.album].avgRuntime = +(
albumAverageRuntime[song.album].totalRuntime /
albumAverageRuntime[song.album].songCount
).toFixed(2);
}
// Update avgRuntime object if the the average is greater than longestAvgRuntime
if (albumAverageRuntime[song.album].avgRuntime > longestAvgRuntime) {
// Update the longestAvgRuntime variable
longestAvgRuntime = albumAverageRuntime[song.album].avgRuntime;
// Update albumName variable
albumName = song.album;
}
});

return albumName;
}
console.log(findAlbumWithLongestAverageRuntime(exampleSongData));
// Problem #18
/**
* Logs song titles sorted by their runtime.
* @param {Object[]} songs - An array of songs.
*/
function printSongsSortedByRuntime(songs) {}
function printSongsSortedByRuntime(songs) {
let songsSortedByRuntime = songs.sort(
(songA, songB) => songA.runtimeInSeconds - songB.runtimeInSeconds
);
songsSortedByRuntime.forEach((song) => console.log(song.title));
}

// Problem #19
/**
* Prints a summary of each album, including its name, total runtime, and number of songs.
* @param {Object[]} songs - An array of songs.
*/
function printAlbumSummaries(songs) {}
function printAlbumSummaries(songs) {
let albumSummaries = {};
songs.forEach((song) => {
if (!albumSummaries[song.album]) {
albumSummaries[song.album] = {
songCount: 1,
totalRuntime: song.runtimeInSeconds,
};
} else {
albumSummaries[song.album].songCount++;
albumSummaries[song.album].totalRuntime += song.runtimeInSeconds;
}
});
for (const summary in albumSummaries) {
console.log(
`${summary}: ${albumSummaries[summary].songCount} songs, Total Runtime: ${albumSummaries[summary].totalRuntime} seconds`
);
}
}

// Problem #20
/**
Expand All @@ -169,11 +294,10 @@ function printAlbumSummaries(songs) {}
*/
function findArtistWithMostSongs(songs) {}


module.exports = {
getSortedTitles,
getSongsFromAlbum,
categorizeSongsByRuntime,
categorizeSongsByRuntime,
findAlbumWithMostSongs,
getFirstSongInAlbum,
isThereLongSong,
Expand All @@ -190,5 +314,5 @@ module.exports = {
findAlbumWithLongestAverageRuntime,
printSongsSortedByRuntime,
printAlbumSummaries,
findArtistWithMostSongs
};;
findArtistWithMostSongs,
};