🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of slaymance

slaymance's solution

to Armstrong Numbers in the JavaScript Track

Published at Jun 05 2021 · 5 comments
Test suite

An Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits.

For example:

  • 9 is an Armstrong number, because 9 = 9^1 = 9
  • 10 is not an Armstrong number, because 10 != 1^2 + 0^2 = 1
  • 153 is an Armstrong number, because: 153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153
  • 154 is not an Armstrong number, because: 154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190

Write some code to determine whether a number is an Armstrong number.


Go through the setup instructions for Javascript to install the necessary dependencies:



Please cd into exercise directory before running all below commands.

Install assignment dependencies:

$ npm install

Making the test suite pass

Execute the tests with:

$ npm test

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by changing xtest to test.

Submitting Solutions

Once you have a solution ready, you can submit it using:

exercism submit armstrong-numbers.js

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

Exercise Source Credits

Wikipedia https://en.wikipedia.org/wiki/Narcissistic_number


import { isArmstrongNumber } from './armstrong-numbers';

describe('Armstrong Numbers', () => {
  test('Zero is an Armstrong number', () => {

  xtest('Single digit numbers are Armstrong numbers', () => {

  xtest('There are no 2 digit Armstrong numbers', () => {

  xtest('Three digit number that is an Armstrong number', () => {

  xtest('Three digit number that is not an Armstrong number', () => {

  xtest('Four digit number that is an Armstrong number', () => {

  xtest('Four digit number that is not an Armstrong number', () => {

  xtest('Seven digit number that is an Armstrong number', () => {

  xtest('Seven digit number that is not an Armstrong number', () => {
 * Check out all my solutions to the Exercism JavaScript track:
 * github.com/slaymance/exercism/tree/main/javascript

export const isArmstrongNumber = num => [...`${num}`]
  .reduce((sum, digit, _, { length }) => sum + digit ** length, 0) === num;

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of danchenkov

Brilliant use of reduce! I am trying to find the documentation for the use of { length } array as the 4th parameter. I understand that it works, but curious what length is applied to in this case, would appreciate any reference.

Avatar of slaymance

@danchenkov Glad you like the solution! The fourth parameter to .reduce is always a reference to the source array upon which the reduce method is being called. In this case, I'm using object destructuring to get the length property of the source array to aid in the calculation of Armstrong numbers. It's functionally equivalent to:

.reduce((sum, digit, _, srcArray) => sum + digit ** srcArray.length, 0)
Avatar of slaymance

@JakubBorowki917 It looks like your solution is working with one minor bug that causes the whole thing to break: the .reduce callback needs an initial value. If no initial value is provided to .reduce then the first value in the array is used as the accumulator and the callback logic begins on the second element in the array.

So in your solution, the first element of the array you create is never being raised to the length of the number passed into the function.

Avatar of jasper2virtual

oh my god! you are very clever

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?