Last active
November 17, 2017 18:55
-
-
Save airportyh/203f7c2d8a8b29af44218b79596af32f 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 main(authors) { | |
authors.forEach(function(author) { | |
let name, articles; | |
if (author) { | |
if (author.name) { | |
name = author.name; | |
} else if (author.firstName) { | |
if (author.lastName) { | |
name = author.firstName + ' ' + author.lastName; | |
} else { | |
name = author.firstName; | |
} | |
} | |
} | |
if (!name) { | |
name = '<Unknown author>'; | |
} | |
if (author) { | |
articles = author.articles; | |
} | |
if (!articles) { | |
articles = []; | |
} | |
let articleCount = articles.length; | |
let maybeS = articleCount > 1 || articleCount === 0 ? 's' : ''; | |
console.log(`${name} has published ${articles.length} article${maybeS}.`); | |
articles.forEach(function(article) { | |
let title, wordCount; | |
if (article) { | |
title = article.title; | |
} | |
if (!title) { | |
title = '<Untitled>'; | |
} | |
if (article) { | |
if (article.contents) { | |
let words = article.contents | |
.split(/[^A-Za-z']+/) | |
.filter(word => !!word); | |
wordCount = words.length; | |
} | |
} | |
if (!wordCount) { | |
wordCount = 0; | |
} | |
console.log(` Article ${title} has ${wordCount} words.`); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment