Untitled
unknown
plain_text
4 years ago
2.3 kB
6
Indexable
let Songs = [ { title: "Love Don't Let Me Go", year: 2021, artist: "David Guetta" }, { title: "Take It Off", year: 2020, artist: "kesha" }, { title: "SICKO MODE", year: 2018, artist: "Travis Scott" }, { title: "BAD GUY", year: 2019, artist: "Billie Eilish" }, { title: "So Good", year: 2017, artist: "B.o.B" }, { title: "Thinking Out Loud", year: 2015, artist: "Ed Sheeran" }, { title: "Happy", year: 2014, artist: "Pharrell Williams" }, { title: "Let Her Go", year: 2013, artist: "Passenger" }, { title: "StarShips", year: 2012, artist: "Nicki Minaj" }, { title: "The Domino", year: 2012, artist: " Jessie J" } ] function formatData(obj){ let {title, year, artist} = obj; return `${title} (${year}) by ${ artist}` } function printData(arr){ console.log(`Song Titles:\n`); // Produces a new array that only contains string values created by the above function let data = arr.map(item => formatData(item)); // sorting the data array data.sort(); // printing each item from the sorted data array data.forEach(item => console.log(item)); } printData(Songs); function SortSong(value){ console.log(`\nSongs newer than ${value}:\n`); // Using the SortSong function to get only the items that meet the condition let data = Songs.filter(item => item.year > value); if(data.length > 0){ data.forEach(item => console.log(formatData(item))); } else{ console.log('\nThere are no Songs that match the criteria.\n'); } } // Filter Songs released by year SortSong(2015); // Average age of the Songs function average(arr){ let date = new Date(); // Fetching the year let year = date.getFullYear(); // Using reduce to find the total age of all the Songs let totalAge = arr.reduce((a,b) => a + year - b.year,0); // Calculating average let average = Math.round((totalAge/arr.length)); console.log(`\nThe average age of these Songs is ${average} years.\n`); } average(Songs);
Editor is loading...