All files / leetCode 0202.ts

100% Statements 13/13
100% Branches 2/2
100% Functions 2/2
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26  39x   39x 71x 71x     39x       3x 3x   3x 13x 13x     3x 2x   1x    
function squareNum(n: number): number {
  let sum = 0
 
  while (n) {
    sum += (n % 10) * (n % 10)
    n = Math.floor(n / 10)
  }
 
  return sum
}
 
export default function isHappy(n: number): boolean {
  let slow = n
  let fast = n
 
  do {
    slow = squareNum(slow)
    fast = squareNum(squareNum(fast))
  } while (slow !== fast)
 
  if (slow === 1)
    return true
  else
    return false
}