Upload 4.7
This commit is contained in:
37
part4/bloglist/utils/list_helper.js
Normal file
37
part4/bloglist/utils/list_helper.js
Normal file
@@ -0,0 +1,37 @@
|
||||
const dummy = (blogs) => {
|
||||
return 1
|
||||
}
|
||||
|
||||
const sumLikes = (blogs) => {
|
||||
return blogs.reduce((sum, blog) => sum + blog.likes, 0)
|
||||
}
|
||||
|
||||
const favoriteBlog = (blogs) => {
|
||||
return blogs.reduce((max, blog) => max.likes > blog.likes ? max : blog)
|
||||
}
|
||||
|
||||
const mostBlogs = (blogs) => {
|
||||
const authors = blogs.map(blog => blog.author)
|
||||
const uniqueAuthors = [...new Set(authors)]
|
||||
const authorBlogs = uniqueAuthors.map(author => {
|
||||
return { author: author, blogs: authors.filter(a => a === author).length }
|
||||
})
|
||||
return authorBlogs.reduce((max, author) => max.blogs > author.blogs ? max : author)
|
||||
}
|
||||
|
||||
const mostLikes = (blogs) => {
|
||||
const authors = blogs.map(blog => blog.author)
|
||||
const uniqueAuthors = [...new Set(authors)]
|
||||
const authorLikes = uniqueAuthors.map(author => {
|
||||
return { author: author, likes: blogs.filter(blog => blog.author === author).reduce((sum, blog) => sum + blog.likes, 0) }
|
||||
})
|
||||
return authorLikes.reduce((max, author) => max.likes > author.likes ? max : author)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
dummy,
|
||||
sumLikes,
|
||||
favoriteBlog,
|
||||
mostBlogs,
|
||||
mostLikes
|
||||
}
|
||||
Reference in New Issue
Block a user