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
1.5 KiB
1.5 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 69cfca90e8a0a6d4d6871c52 | Challenge 268: Narcissistic Number | 28 | 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 13 + 53 + 33 = 153, so it is narcissistic.
--hints--
isNarcissistic(153) should return true.
assert.isTrue(isNarcissistic(153));
isNarcissistic(154) should return false.
assert.isFalse(isNarcissistic(154));
isNarcissistic(371) should return true.
assert.isTrue(isNarcissistic(371));
isNarcissistic(512) should return false.
assert.isFalse(isNarcissistic(512));
isNarcissistic(9) should return true.
assert.isTrue(isNarcissistic(9));
isNarcissistic(11) should return false.
assert.isFalse(isNarcissistic(11));
isNarcissistic(9474) should return true.
assert.isTrue(isNarcissistic(9474));
isNarcissistic(6549) should return false.
assert.isFalse(isNarcissistic(6549));
--seed--
--seed-contents--
function isNarcissistic(n) {
return n;
}
--solutions--
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;
}