-
-
Save haru01/94db74d1bca5b3c1968ac34205e118e4 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) { | |
return renderPlainText(readList, recommendList); | |
} | |
function renderPlainText(readList, recommendList) { | |
let report = `name: ${readList.name}\n`; | |
report += '-----\n'; | |
for (let readBook of readList.books) { | |
if (readBook.times >= 1) { | |
report += ` - ${readBook.name}: ${point(readBook)} point\n`; | |
} | |
} | |
report += '-----\n'; | |
report += `total: ${total()} point`; | |
return report; | |
function total() { | |
return readList.books | |
.reduce((total, book) => total + point(book), 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); | |
} | |
} | |
} | |
module.exports = generateReadReport; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment