2020-05-03 16:33:07 -07:00
---
id: 5eaf48389ee512d4d103684b
2020-11-27 19:02:05 +01:00
title: Self Describing Numbers
2020-05-03 16:33:07 -07:00
challengeType: 5
2020-06-08 20:07:41 +05:30
forumTopicId: 385289
2021-01-13 03:31:00 +01:00
dashedName: self-describing-numbers
2020-05-03 16:33:07 -07:00
---
2020-11-27 19:02:05 +01:00
# --description--
There are several so-called "self describing" or ["self-descriptive" ](https://en.wikipedia.org/wiki/Self-descriptive_number ) integers.
2020-05-03 16:33:07 -07:00
2020-05-05 11:16:09 -07:00
An integer is said to be "self-describing" if it has the property that, when digit positions are labeled 0 to N-1, the digit in each position is equal to the number of times that digit appears in the number.
2020-05-03 16:33:07 -07:00
2020-11-27 19:02:05 +01:00
For example, **2020** is a four-digit self describing number:
2020-05-03 16:33:07 -07:00
< ul >
< li > position 0 has value 2 and there are two 0s in the number; < / li >
< li > position 1 has value 0 and there are no 1s in the number; < / li >
< li > position 2 has value 2 and there are two 2s; < / li >
< li > position 3 has value 0 and there are zero 3s; < / li >
< / ul >
2020-11-27 19:02:05 +01:00
Self-describing numbers < 100,000,000 are: 1210, 2020, 21200, 3211000, 42101000.
2020-05-03 16:33:07 -07:00
2020-11-27 19:02:05 +01:00
# --instructions--
2020-05-03 16:33:07 -07:00
2020-05-05 11:16:09 -07:00
Write a function that takes a positive integer as a parameter. If it is self-describing return true. Otherwise, return false.
2020-05-03 16:33:07 -07:00
2020-11-27 19:02:05 +01:00
# --hints--
`isSelfDescribing` should be a function.
```js
assert(typeof isSelfDescribing == 'function');
2020-05-03 16:33:07 -07:00
```
2020-11-27 19:02:05 +01:00
`isSelfDescribing()` should return a boolean.
2020-05-03 16:33:07 -07:00
2020-11-27 19:02:05 +01:00
```js
assert(typeof isSelfDescribing(2020) == 'boolean');
```
2020-05-03 16:33:07 -07:00
2020-11-27 19:02:05 +01:00
`isSelfDescribing(2020)` should return `true` .
2020-05-03 16:33:07 -07:00
```js
2020-11-27 19:02:05 +01:00
assert.equal(isSelfDescribing(2020), true);
```
2020-09-15 09:57:40 -07:00
2020-11-27 19:02:05 +01:00
`isSelfDescribing(3021)` should return `false` .
```js
assert.equal(isSelfDescribing(3021), false);
2020-05-03 16:33:07 -07:00
```
2020-11-27 19:02:05 +01:00
`isSelfDescribing(3211000)` should return `true` .
2020-05-03 16:33:07 -07:00
2020-11-27 19:02:05 +01:00
```js
assert.equal(isSelfDescribing(3211000), true);
```
2020-05-03 16:33:07 -07:00
2020-11-27 19:02:05 +01:00
# --seed--
2020-05-03 16:33:07 -07:00
2020-11-27 19:02:05 +01:00
## --seed-contents--
```js
function isSelfDescribing(n) {
}
```
# --solutions--
2020-05-03 16:33:07 -07:00
```js
function isSelfDescribing(n) {
let digits = String(n).split("");
digits = digits.map(function(e) {return parseInt(e)});
let count = digits.map((x) => {return 0})
digits.forEach((d) =>{
if (d >= count.length) {
return false
}
count[d] += 1;
});
if (digits === count) {
return true;
}
if (digits.length != count.length) {
return false;
}
for (let i=0; i< digits.length ; i + + ) {
if (digits[i] !== count[i]) {
return false;
}
}
return true;
}
```