Files
freeCodeCamp/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/happy-numbers.english.md
Randell Dawson c25916c9a2 fix(curriculum): changed test text to use should for Coding Interview Prep - part 2 of 2 (#37766)
* fix: changed test text to use should

* fix: corrected typo

Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com>

* fix: removed extra period

Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com>

* fix: removed extra period

Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com>

* fix: removed extra period

Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com>

* fix: removed extra period

Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com>

* fix: corrected typo

Co-Authored-By: Tom <20648924+moT01@users.noreply.github.com>
2019-11-20 10:01:31 -05:00

2.5 KiB

title, id, challengeType, forumTopicId
title id challengeType forumTopicId
Happy numbers 594810f028c0303b75339ad1 5 302280

Description

A happy number is defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are unhappy numbers.

Instructions

Implement a function that returns true if the number is happy, or false if not.

Tests

tests:
  - text: <code>happy</code> should be a function.
    testString: assert(typeof happy === 'function');
  - text: <code>happy(1)</code> should return a boolean.
    testString: assert(typeof happy(1) === 'boolean');
  - text: <code>happy(1)</code> should return true.
    testString: assert(happy(1));
  - text: <code>happy(2)</code> should return false.
    testString: assert(!happy(2));
  - text: <code>happy(7)</code> should return true.
    testString: assert(happy(7));
  - text: <code>happy(10)</code> should return true.
    testString: assert(happy(10));
  - text: <code>happy(13)</code> should return true.
    testString: assert(happy(13));
  - text: <code>happy(19)</code> should return true.
    testString: assert(happy(19));
  - text: <code>happy(23)</code> should return true.
    testString: assert(happy(23));
  - text: <code>happy(28)</code> should return true.
    testString: assert(happy(28));
  - text: <code>happy(31)</code> should return true.
    testString: assert(happy(31));
  - text: <code>happy(32)</code> should return true:.
    testString: assert(happy(32));
  - text: <code>happy(33)</code> should return false.
    testString: assert(!happy(33));

Challenge Seed

function happy(number) {
  // Good luck!
}

Solution

function happy (number) {
  let m;
  let digit;
  const cycle = [];

  while (number !== 1 && cycle[number] !== true) {
    cycle[number] = true;
    m = 0;
    while (number > 0) {
      digit = number % 10;
      m += Math.pow(digit, 2);
      number = (number - digit) / 10;
    }
    number = m;
  }
  return (number === 1);
}