CLooker

hackerRank jumping on the clouds revisited

Jan 28th, 2018
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // https://www.hackerrank.com/challenges/jumping-on-the-clouds-revisited/problem
  2.  
  3. // declarative
  4. const jumpingOnClouds = (c, k) => (
  5.   c.reduce(({ energy, ind }, cloud, idx) => (
  6.       idx === ind
  7.           ? cloud === 0
  8.             ? {energy: energy - 1, ind: ind + k}
  9.             : {energy: energy - 3, ind: ind + k}                
  10.           : {energy, ind}
  11.   ), {energy: 100, ind: 0}).energy
  12. )
  13.  
  14. // imperative
  15. function jumpingOnClouds(c, k) {
  16.     let e = 100;
  17.     for (let i = 0; i < c.length; i = i + k) {
  18.         if (c[i] === 0) {
  19.             e = e - 1;
  20.         }
  21.         else {
  22.             e = e - 3;
  23.         }
  24.     }
  25.     return e;
  26. }
Add Comment
Please, Sign In to add comment