Skip to content

Instantly share code, notes, and snippets.

@airportyh
Last active November 17, 2017 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save airportyh/203f7c2d8a8b29af44218b79596af32f to your computer and use it in GitHub Desktop.
Save airportyh/203f7c2d8a8b29af44218b79596af32f to your computer and use it in GitHub Desktop.
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