-
Notifications
You must be signed in to change notification settings - Fork 0
/
Especially Joyful Numbers
25 lines (21 loc) · 994 Bytes
/
Especially Joyful Numbers
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
/*
Positive integers that are divisible exactly by the sum of their digits are called Harshad numbers. The first few Harshad numbers are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, ...
We are interested in Harshad numbers where the product of its digit sum s and s with the digits reversed, gives the original number n. For example consider number 1729:
its digit sum, s = 1 + 7 + 2 + 9 = 19
reversing s = 91
and 19 * 91 = 1729 --> the number that we started with.
Complete the function which tests if a positive integer n is Harshad number, and returns True if the product of its digit sum and its digit sum reversed equals n; otherwise return False.
*/
function numberJoy(n) {
// spread out numbers
//add them up
//reverse them
//see if reverse num = num
let sum = n.toString().split('').map(num => Number(num)).reduce((acc,c) => acc + c,0)
let reverse = sum.toString().split('').reverse('').join('')
if (sum * reverse === n) {
return true
} else {
return false
}
}