Guest User

Untitled

a guest
Nov 19th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. function main(authors) {
  2. authors.forEach(function(author) {
  3. let name, articles;
  4. if (author) {
  5. if (author.name) {
  6. name = author.name;
  7. } else if (author.firstName) {
  8. if (author.lastName) {
  9. name = author.firstName + ' ' + author.lastName;
  10. } else {
  11. name = author.firstName;
  12. }
  13. }
  14. }
  15. if (!name) {
  16. name = '<Unknown author>';
  17. }
  18. if (author) {
  19. articles = author.articles;
  20. }
  21. if (!articles) {
  22. articles = [];
  23. }
  24. let articleCount = articles.length;
  25. let maybeS = articleCount > 1 || articleCount === 0 ? 's' : '';
  26. console.log(`${name} has published ${articles.length} article${maybeS}.`);
  27. articles.forEach(function(article) {
  28. let title, wordCount;
  29. if (article) {
  30. title = article.title;
  31. }
  32. if (!title) {
  33. title = '<Untitled>';
  34. }
  35. if (article) {
  36. if (article.contents) {
  37. let words = article.contents
  38. .split(/[^A-Za-z']+/)
  39. .filter(word => !!word);
  40. wordCount = words.length;
  41. }
  42. }
  43. if (!wordCount) {
  44. wordCount = 0;
  45. }
  46. console.log(` Article ${title} has ${wordCount} words.`);
  47. });
  48. });
  49. }
Add Comment
Please, Sign In to add comment