-
Notifications
You must be signed in to change notification settings - Fork 1
/
narcissisticNumber.js
43 lines (30 loc) · 1.35 KB
/
narcissisticNumber.js
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// 'Does my number look big in this?
// from https://www.codewars.com/kata/does-my-number-look-big-in-this/train/javascript
/*A Narcissistic Number is a number which is the sum of its own digits, each raised to the power of the number of digits.
For example, take 153 (3 digits):
1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
and 1634 (4 digits):
1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 256 = 1634
The Challenge:
Your code must return true or false depending upon whether the given number is a Narcissistic number.
Error checking for text strings or other invalid inputs is not required, only valid integers will be passed into the function.*/
/*eslint-disable eqeqeq*/
const narcissistic = value => `${value}`.split('').reduce((a, b) => a + Math.pow(b, `${value}`.length), 0) == value;
const expect = require('chai').expect;
describe('narcissistic function', () => {
context('all one-digit numbers are narcissistic', () => {
it('should return true given 7', () => {
expect(narcissistic(7)).to.be.true;
});
});
context('some larger numbers are also narcissistic', () => {
it('should return true given 371', () => {
expect(narcissistic(371)).to.be.true;
});
});
context('other large numbers are not narcissistic', () => {
it('should return false given 144', () => {
expect(narcissistic(144)).to.be.false;
});
});
});