Last active
April 4, 2019 01:54
-
-
Save haru01/a8f9a8adfc953596dbbbd47b8dc0414b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function generateReadReport(readList, recommendList) { | |
const reportData = createReportData(readList, recommendList); | |
return renderPlainText(reportData); | |
} | |
function createReportData(readList, recommendList) { | |
const reportData = {}; | |
reportData.userName = readList.name; | |
reportData.readBooks = readList.books | |
.map(book => { | |
return {...book, point: point(book)}; | |
}); | |
reportData.total = total(reportData); | |
return reportData; | |
function total(reportData) { | |
return reportData.readBooks | |
.reduce((total, book) => total + book.point, 0); | |
} | |
function point(readBook) { | |
if (readBook.times <= 0) { | |
return 0; | |
} | |
if (notRecommendBook()) { | |
return 1; | |
} | |
if (readBook.times >= 3) { | |
return 8; | |
} else if (readBook.times === 2) { | |
return 5; | |
} else if (readBook.times === 1) { | |
return 3; | |
} else { | |
return 0; | |
} | |
function notRecommendBook() { | |
return !recommendList.map(recommend => recommend.asin).includes(readBook.asin); | |
} | |
} | |
} | |
function renderPlainText(reportData) { | |
let report = `name: ${reportData.userName}\n`; | |
report += '-----\n'; | |
for (let book of reportData.readBooks) { | |
if (book.times >= 1) { | |
report += ` - ${book.name}: ${book.point} point\n`; | |
} | |
} | |
report += '-----\n'; | |
report += `total: ${reportData.total} point`; | |
return report; | |
} | |
module.exports = generateReadReport; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment