Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. type Game = {
  2. name: string
  3. rating: number
  4. }
  5.  
  6. const gameList: Game[] = [
  7. { name: 'call of duty', rating: 4.0 },
  8. { name: 'assasin creed', rating: 6.4 },
  9. { name: 'mario', rating: 10.2 },
  10. ]
  11.  
  12. // 1. returns list of name from list of game
  13. const names = (games: Game[]): string[] =>
  14. games.map(game => game.name)
  15.  
  16. // 2. returns list of string that has format `<name> [<rating>]`
  17. // eg. `call of duty [7.4]`
  18. const formatGames = (games: Game[]): string[] =>
  19. games.map(({ name, rating }) => `${name} [${rating}]`)
  20.  
  21. // 3. get sum of all the ratings in list of game
  22. const totalRatingsOf = (games: Game[]): number =>
  23. games
  24. .map(game => game.rating)
  25. .reduce((a, b) => a + b, 0)
  26.  
  27. // 4. get average of all the ratings in list of game
  28. const averageRatingsOf = (games: Game[]): number =>
  29. totalRatingsOf(games) / games.length
  30.  
  31. // ----
  32.  
  33. const gamesWithName = (name: string, games: Game[]): Game[] =>
  34. games.filter(game => game.name === name)
  35.  
  36. // 5. get total rating by names from list of game
  37. const ratingByName = (name: string, games: Game[]): number =>
  38. totalRatingsOf(gamesWithName(name, games))
  39.  
  40. // 6. get total rating by names from list of game
  41. const averageRatingsByName = (name: string, games: Game[]): number =>
  42. averageRatingsOf(gamesWithName(name, games))
  43.  
  44. // ---
  45.  
  46. const updateGameRating = (update: (rating: number) => number) => (game: Game) => ({
  47. ...game,
  48. rating: update(game.rating)
  49. })
  50.  
  51. // 7. increase rating for all games
  52. const increaseRatingBy = (inc: number, games: Game[]): Game[] =>
  53. games
  54. .map(updateGameRating(rating => rating + inc))
  55.  
  56. // 8. decrease rating for game with specific name
  57. const decreaseRatingBy = (
  58. dec: number,
  59. name: string,
  60. games: Game[],
  61. ): Game[] =>
  62. gamesWithName(name, games)
  63. .map(updateGameRating(rating => rating - dec))
  64.  
  65. // 9. returns game if the name exist else returns undefined
  66. const findByName = (name: string, games: Game[]): Game =>
  67. games.find(game => game.name === name)
  68.  
  69. // games.reduce((found, game) =>
  70. // found
  71. // ? found
  72. // : game.name === name
  73. // ? game
  74. // : undefined
  75. // , undefined)
  76.  
  77. console.log(findByName('call of duty', gameList))
  78.  
  79. // 10. decrease rating for game that has name in the list
  80. const decreaseRatingForNames = (
  81. dec: number,
  82. names: string[],
  83. games: Game[],
  84. ): Game[] =>
  85. games
  86. .filter(game => names.includes(game.name))
  87. .map(updateGameRating(rating => rating - dec))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement