DEV Community

Discussion on: Write a script to find "Happy Numbers"

Collapse
 
nickytonline profile image
Nick Taylor

You could even shorten the toString by replacing it with a template string which will do an implicit toString.

const isHappy = n => {
  if (n === 4) return false;

  const u = [...`${n}`].reduce((ac, c) => ac + (c ** 2), 0);

  return u === 1 || isHappy(u);
};