CLooker

hackerRank viral advertising

Jan 28th, 2018
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // https://www.hackerrank.com/challenges/strange-advertising/problem
  2.  
  3. // declarative
  4. const viralAdvertising = n => (
  5.   Array.from(Array(n))
  6.     .reduce((days, _, i) => days.concat(i + 1), [])
  7.     .reduce((dailyLikeTotal, day) => (
  8.       day === 1
  9.         ? dailyLikeTotal.concat(2)
  10.         : dailyLikeTotal.concat(Math.floor((dailyLikeTotal[dailyLikeTotal.length - 1] * 3) / 2))
  11.     ), [])
  12.     .reduce((totalLikes, dailyLikes) => totalLikes + dailyLikes)
  13. )
  14.  
  15. // imperative
  16. function viralAdvertising(n) {
  17.     let peopleWhoLikeIt = [];
  18.     for (let i = 1; i <= n; i++) {
  19.         if (i === 1) {
  20.             peopleWhoLikeIt.push(2);
  21.         } else {
  22.             peopleWhoLikeIt.push(Math.floor((peopleWhoLikeIt[peopleWhoLikeIt.length - 1] * 3) / 2))
  23.         }
  24.     }
  25.    
  26.     let total = 0;
  27.     for (let i = 0; i < peopleWhoLikeIt.length; i++) {
  28.         total = total + peopleWhoLikeIt[i];
  29.     }
  30.     return total;
  31. }
Add Comment
Please, Sign In to add comment