dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
87 lines
1.5 KiB
Markdown
87 lines
1.5 KiB
Markdown
---
|
|
id: 69cfca90e8a0a6d4d6871c52
|
|
title: "Challenge 268: Narcissistic Number"
|
|
challengeType: 28
|
|
dashedName: challenge-268
|
|
---
|
|
|
|
# --description--
|
|
|
|
Given a positive integer, determine whether it is a narcissistic number.
|
|
|
|
- A number is narcissistic if the sum of each of its digits raised to the power of the total number of digits equals the number itself.
|
|
|
|
For example, 153 has 3 digits, and 1<sup>3</sup> + 5<sup>3</sup> + 3<sup>3</sup> = 153, so it is narcissistic.
|
|
|
|
# --hints--
|
|
|
|
`isNarcissistic(153)` should return `true`.
|
|
|
|
```js
|
|
assert.isTrue(isNarcissistic(153));
|
|
```
|
|
|
|
`isNarcissistic(154)` should return `false`.
|
|
|
|
```js
|
|
assert.isFalse(isNarcissistic(154));
|
|
```
|
|
|
|
`isNarcissistic(371)` should return `true`.
|
|
|
|
```js
|
|
assert.isTrue(isNarcissistic(371));
|
|
```
|
|
|
|
`isNarcissistic(512)` should return `false`.
|
|
|
|
```js
|
|
assert.isFalse(isNarcissistic(512));
|
|
```
|
|
|
|
`isNarcissistic(9)` should return `true`.
|
|
|
|
```js
|
|
assert.isTrue(isNarcissistic(9));
|
|
```
|
|
|
|
`isNarcissistic(11)` should return `false`.
|
|
|
|
```js
|
|
assert.isFalse(isNarcissistic(11));
|
|
```
|
|
|
|
`isNarcissistic(9474)` should return `true`.
|
|
|
|
```js
|
|
assert.isTrue(isNarcissistic(9474));
|
|
```
|
|
|
|
`isNarcissistic(6549)` should return `false`.
|
|
|
|
```js
|
|
assert.isFalse(isNarcissistic(6549));
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
function isNarcissistic(n) {
|
|
|
|
return n;
|
|
}
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
function isNarcissistic(n) {
|
|
const digits = String(n).split('');
|
|
const power = digits.length;
|
|
const sum = digits.reduce((acc, d) => acc + Math.pow(Number(d), power), 0);
|
|
return sum === n;
|
|
}
|
|
```
|