diff --git a/README.md b/README.md index e15a251..86f06b1 100644 --- a/README.md +++ b/README.md @@ -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: @@ -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:** @@ -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 @@ -38,11 +41,13 @@ - 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 @@ -50,6 +55,7 @@ - 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 @@ -57,6 +63,7 @@ - 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 . @@ -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. - diff --git a/index.js b/index.js index 2bb3c16..f4aabbe 100644 --- a/index.js +++ b/index.js @@ -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. @@ -15,6 +13,9 @@ const exampleSongData = require("./data/songs"); */ function getSortedTitles(songs) {} + + + // #2 /** * Returns the titles of all songs from a specified album. @@ -22,9 +23,38 @@ function getSortedTitles(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. @@ -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 /** @@ -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. @@ -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 /** @@ -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 /** @@ -169,11 +294,10 @@ function printAlbumSummaries(songs) {} */ function findArtistWithMostSongs(songs) {} - module.exports = { getSortedTitles, getSongsFromAlbum, - categorizeSongsByRuntime, + categorizeSongsByRuntime, findAlbumWithMostSongs, getFirstSongInAlbum, isThereLongSong, @@ -190,5 +314,5 @@ module.exports = { findAlbumWithLongestAverageRuntime, printSongsSortedByRuntime, printAlbumSummaries, - findArtistWithMostSongs -};; + findArtistWithMostSongs, +};