JavaScript: Happy numbers

Last update: 01 Jun 23:20
1
Student

Happy numbers

Let's call happy numbers those that, as a result of a series of transformations of the form "sum of squares of digits", will turn into one. For example:

7   => 7^2 = 49,
49  => 4^2 + 9^2 = 16 + 81 = 97,
97  => 9^2 + 7^2 = 81 + 49 = 130,
130 => 1^2 + 3^2 + 0^2 = 10,
10  => 1^2 + 0^2 = 1.

Conclusion: the initial number 7 is a happy one.

isHappyNumber.js

Implement and export as default a function that should return true if the number is happy, and false if not. The number of search iterations should be limited to 10.

Tips

  • Use the auxiliary function sumOfSquareDigits() that which should take a number as input and return the "sum of squares of digits" of this number
  • Use str.length to get a string length

For full access to the challenge you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests