Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type Game = { name: string, rating: number }
- const listOfgame:Game[] =[
- {name:'redalert' , rating:50},
- {name:'devilcry', rating:2},
- {name:'dota2', rating:3},
- {name:'dota2',rating:5}
- ]
- // 1. returns list of name from list of game
- const names = (games: Game[]): string[] => games.map((game) => game.name)
- console.log(names(listOfgame))
- // 2. returns list of string that has format `<name> [<rating>]`
- // eg. `call of duty [7.4]`
- const formatGames = (games: Game[]): string[] => games.map((game) => {
- return `${game.name} [${game.rating}]`
- })
- console.log(formatGames(listOfgame))
- // 3. get sum of all the ratings in list of game
- const totalRatingsOf = (games: Game[]): number => games.reduce((sum,game) =>{
- return sum + game.rating
- }, 0)
- console.log(totalRatingsOf(listOfgame))
- // 4. get average of all the ratings in list of game
- const averageRatingsOf = (games: Game[]): number => games.reduce((sumaverage,game) => {
- return sumaverage + game.rating
- }, 0)/games.length
- console.log(averageRatingsOf(listOfgame))
- // 5. get total rating by name from list of game
- const totalRatingsByName = (name: string, games: Game[]): number =>
- totalRatingsOf(games.filter( game => game.name === name ) )
- console.log(totalRatingsByName('dota2',listOfgame))
- // 6. get total rating by name from list of game
- const averageRatingsByName = (name: string, games: Game[]): number => totalRatingsOf(games.filter( game =>
- game.name === name
- ))/games.length
- console.log(averageRatingsByName('dota2',listOfgame))
- // 7. increase rating for all games
- const increaseRatingBy = (inc: number, games: Game[]): Game[] =>
- games.map((game) => {return { name: game.name, rating:game.rating + inc } } )
- console.log(increaseRatingBy(1,listOfgame))
- // 8. decrease rating for game with specific name
- const decreaseRatingBy = (dec: number, name: string, games: Game[]): Game[] =>
- games.filter((game => game.name === name )).map((game => {
- return{
- name: game.name,
- rating: game.rating - dec
- }
- }
- ))
- console.log(decreaseRatingBy(20,'redalert',listOfgame))
- // 9. returns game if the name exist else returns undefined
- const findByName = (name: string, games: Game[]): Game[] => games.reduce((game => {
- return game.name=== name ?game :'undefined'}
- }))
- // const findByName = (name: string, games: Game[]): Game[] => games.filter(game =>{
- // if( game.name === name ) {
- // return game
- // }
- // else {return undefined
- // }
- // })
- console.log(findByName('redaledrt',listOfgame))
- // 10. decrease rating for game that has name in the list
- const decreaseRatingForNames = (dec: number, names: string[], games: Game[]): Game[] =>
- games.filter((game => game.name === names )).map((game => {
- return{
- name: game.name,
- rating: game.rating - dec
- }
- }
- ))
- console.log(decreaseRatingForNames(6734,'dota2',listOfgame))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement