Skip to content

Instantly share code, notes, and snippets.

@desinas
Last active May 25, 2019 15:52
Show Gist options
  • Save desinas/33cfb264d0c8b34ca7e9b320f5eb39b2 to your computer and use it in GitHub Desktop.
Save desinas/33cfb264d0c8b34ca7e9b320f5eb39b2 to your computer and use it in GitHub Desktop.
Callback Functions at Runtime by Udacity FrontENDev
/* Using filter()
*
* Using the musicData array and filter():
* - Return only album objects where the album's name is
* 10 characters long, 25 characters long, or anywhere in between
* - Store the returned data in a new `results` variable
*
* Note:
* - Do not delete the musicData variable
* - Do not alter any of the musicData content
*/
const musicData = [
{ artist: 'Adele', name: '25', sales: 1731000 },
{ artist: 'Drake', name: 'Views', sales: 1608000 },
{ artist: 'Beyonce', name: 'Lemonade', sales: 1554000 },
{ artist: 'Chris Stapleton', name: 'Traveller', sales: 1085000 },
{ artist: 'Pentatonix', name: 'A Pentatonix Christmas', sales: 904000 },
{ artist: 'Original Broadway Cast Recording',
name: 'Hamilton: An American Musical', sales: 820000 },
{ artist: 'Twenty One Pilots', name: 'Blurryface', sales: 738000 },
{ artist: 'Prince', name: 'The Very Best of Prince', sales: 668000 },
{ artist: 'Rihanna', name: 'Anti', sales: 603000 },
{ artist: 'Justin Bieber', name: 'Purpose', sales: 554000 }
];
const results = musicData.filter( function collect(album) {
return album.name.length > 9 && album.name.length < 26;
} );
console.log(results);
@desinas
Copy link
Author

desinas commented Apr 17, 2018

What Went Well

  • Your code should have a variable musicData
  • The “musicData” variable should be an array
  • Your code should have a variable “albumSalesStrings”
  • Your code should call .map() on “musicData`
  • Your code should produce the expected output

Feedback

Your answer passed all our tests! Awesome job!

@desinas
Copy link
Author

desinas commented Apr 18, 2018

What Went Well

  • Your code should have a variable musicData
  • The “musicData” variable should be an array
  • Your code should call “.filter()” on musicData
  • Your code should have a variable “results”
  • Your code should produce the expected output

Feedback: Your answer passed all our tests! Awesome job!

@desinas
Copy link
Author

desinas commented Apr 18, 2018

Another way to return the same as the first example:

const albumSalesStrings11 = musicData.map(function albuSaleCollect (albuSale) {

    return `${albuSale.name} by ${albuSale.artist} sold ${albuSale.sales} copies`

});

@hotboy01
Copy link

you can also write it like this

const albumSalesStrings = musicData.map(function(music){ return music.name + ' by ' + music.artist + ' sold ' + music.sales + ' copies'; });

@hotboy01
Copy link

sorry thats for the map method

@hotboy01
Copy link

hotboy01 commented Oct 31, 2018

for the above filter method
const results = musicData.filter(function(album){ return album.name.length >= 10 && album.name.length < 25; });

@farooq-dci
Copy link

farooq-dci commented May 25, 2019

for the filter method, this code also possible
const results = musicData.filter(album => album.name.length <= 25 && album.name.length >= 10);
console.log(results);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment