Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. type Game = { name: string, rating: number }
  2. const listOfgame:Game[] =[
  3. {name:'redalert' , rating:50},
  4. {name:'devilcry', rating:2},
  5. {name:'dota2', rating:3},
  6. {name:'dota2',rating:5}
  7. ]
  8.  
  9.  
  10. // 1. returns list of name from list of game
  11. const names = (games: Game[]): string[] => games.map((game) => game.name)
  12. console.log(names(listOfgame))
  13.  
  14.  
  15. // 2. returns list of string that has format `<name> [<rating>]`
  16. // eg. `call of duty [7.4]`
  17.  
  18. const formatGames = (games: Game[]): string[] => games.map((game) => {
  19. return `${game.name} [${game.rating}]`
  20. })
  21. console.log(formatGames(listOfgame))
  22. // 3. get sum of all the ratings in list of game
  23. const totalRatingsOf = (games: Game[]): number => games.reduce((sum,game) =>{
  24. return sum + game.rating
  25. }, 0)
  26.  
  27. console.log(totalRatingsOf(listOfgame))
  28.  
  29. // 4. get average of all the ratings in list of game
  30. const averageRatingsOf = (games: Game[]): number => games.reduce((sumaverage,game) => {
  31.  
  32. return sumaverage + game.rating
  33.  
  34. }, 0)/games.length
  35. console.log(averageRatingsOf(listOfgame))
  36. // 5. get total rating by name from list of game
  37. const totalRatingsByName = (name: string, games: Game[]): number =>
  38. totalRatingsOf(games.filter( game => game.name === name ) )
  39.  
  40.  
  41. console.log(totalRatingsByName('dota2',listOfgame))
  42.  
  43. // 6. get total rating by name from list of game
  44. const averageRatingsByName = (name: string, games: Game[]): number => totalRatingsOf(games.filter( game =>
  45. game.name === name
  46. ))/games.length
  47. console.log(averageRatingsByName('dota2',listOfgame))
  48.  
  49. // 7. increase rating for all games
  50. const increaseRatingBy = (inc: number, games: Game[]): Game[] =>
  51.  
  52. games.map((game) => {return { name: game.name, rating:game.rating + inc } } )
  53.  
  54.  
  55. console.log(increaseRatingBy(1,listOfgame))
  56. // 8. decrease rating for game with specific name
  57. const decreaseRatingBy = (dec: number, name: string, games: Game[]): Game[] =>
  58.  
  59. games.filter((game => game.name === name )).map((game => {
  60. return{
  61. name: game.name,
  62. rating: game.rating - dec
  63. }
  64. }
  65.  
  66. ))
  67.  
  68.  
  69. console.log(decreaseRatingBy(20,'redalert',listOfgame))
  70.  
  71. // 9. returns game if the name exist else returns undefined
  72. const findByName = (name: string, games: Game[]): Game[] => games.reduce((game => {
  73.  
  74.  
  75. return game.name=== name ?game :'undefined'}
  76.  
  77. }))
  78.  
  79.  
  80. // const findByName = (name: string, games: Game[]): Game[] => games.filter(game =>{
  81.  
  82.  
  83. // if( game.name === name ) {
  84. // return game
  85. // }
  86. // else {return undefined
  87. // }
  88.  
  89.  
  90. // })
  91.  
  92.  
  93.  
  94. console.log(findByName('redaledrt',listOfgame))
  95.  
  96. // 10. decrease rating for game that has name in the list
  97. const decreaseRatingForNames = (dec: number, names: string[], games: Game[]): Game[] =>
  98.  
  99. games.filter((game => game.name === names )).map((game => {
  100. return{
  101. name: game.name,
  102. rating: game.rating - dec
  103. }
  104. }
  105.  
  106. ))
  107.  
  108.  
  109. console.log(decreaseRatingForNames(6734,'dota2',listOfgame))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement