G2A Many GEOs
SHARE
TWEET

Untitled

a guest Apr 2nd, 2020 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * @param {number} n
  3.  * @return {boolean}
  4.  */
  5. const reducer = (accumulator, currentValue) => accumulator + currentValue;
  6.  
  7. var getDigits = function(n) {
  8.     let digits = [];
  9.     let sNumber = n.toString();
  10.    
  11.     for (let i = 0, len = sNumber.length; i < len; i += 1) {
  12.         digits.push(+sNumber.charAt(i));
  13.     }
  14.    
  15.     return digits;
  16. }
  17.  
  18. var square = function(n) {
  19.     return n * n;
  20. }
  21.  
  22. var isHappy = function(n) {
  23.     let happy = false;
  24.    
  25.     // Special case for n == 1
  26.     if (n == 1) {
  27.         return true;
  28.     }
  29.    
  30.     while (n != 1) {
  31.         n = getDigits(n).map(x => square(x)).reduce(reducer);
  32.        
  33.         if (n == 1) {
  34.             happy = true;
  35.         }
  36.     }
  37.    
  38.     return happy;
  39. };
RAW Paste Data
Ledger Nano X - The secure hardware wallet
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top