Skip to the editor
JSGroundwork
JSGroundwork handwritten · web dev
←Back to Hashing

Ready to read

JSJavaScript⑂Git◎Interview prepΣDSA in JSSDSystem Design
More topics15›
</>HTML{ }CSS⚛ReactNNext.jsNeNest.jsTSTypeScriptNoNode.js🐳DockerDBSQL & Databases✓Testing🔒Web Security☁Cloud & DevOps◈GraphQL◆Redis☸Kubernetes
☰‹›
Description
All topics›JavaScript›Hashing

Happy Number

beginnerlayer B3 · Hashing5 tests

A positive integer is happy if repeatedly replacing it with the sum of the squares of its digits eventually reaches 1.

If the process never reaches 1 it loops forever in a cycle. Return true if n is happy, otherwise false.

  • 19 is happy: 1+81 = 82, 64+4 = 68, 36+64 = 100, 1+0+0 = 1
  • 2 is not happy — it falls into a repeating cycle

Stuck?

All exercisesPlayground
1234567891011121314
loading the editor…

Nothing yet — hit Run and whatever you log shows up here.

Submit to see how you did.

⌘/Ctrl + Enter run⌘/Ctrl + S save⌘/Ctrl + / comment⌘/Ctrl + F find/replace⌘/Ctrl + D select next matchAlt + click multi-cursorTab indent · ⇧Tab outdentEsc leave fullscreen