Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // https://www.hackerrank.com/challenges/strange-advertising/problem
- // declarative
- const viralAdvertising = n => (
- Array.from(Array(n))
- .reduce((days, _, i) => days.concat(i + 1), [])
- .reduce((dailyLikeTotal, day) => (
- day === 1
- ? dailyLikeTotal.concat(2)
- : dailyLikeTotal.concat(Math.floor((dailyLikeTotal[dailyLikeTotal.length - 1] * 3) / 2))
- ), [])
- .reduce((totalLikes, dailyLikes) => totalLikes + dailyLikes)
- )
- // imperative
- function viralAdvertising(n) {
- let peopleWhoLikeIt = [];
- for (let i = 1; i <= n; i++) {
- if (i === 1) {
- peopleWhoLikeIt.push(2);
- } else {
- peopleWhoLikeIt.push(Math.floor((peopleWhoLikeIt[peopleWhoLikeIt.length - 1] * 3) / 2))
- }
- }
- let total = 0;
- for (let i = 0; i < peopleWhoLikeIt.length; i++) {
- total = total + peopleWhoLikeIt[i];
- }
- return total;
- }
Add Comment
Please, Sign In to add comment