ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# Blaster's solution

## to Armstrong Numbers in the JavaScript Track

Published at Apr 28 2021 · 0 comments
Instructions
Test suite
Solution

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.

## Setup

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

https://exercism.io/tracks/javascript/installation

## Requirements

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

### armstrong-numbers.spec.js

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

describe('Armstrong Numbers', () => {
test('Zero is an Armstrong number', () => {
expect(isArmstrongNumber(0)).toEqual(true);
});

xtest('Single digit numbers are Armstrong numbers', () => {
expect(isArmstrongNumber(5)).toEqual(true);
});

xtest('There are no 2 digit Armstrong numbers', () => {
expect(isArmstrongNumber(10)).toEqual(false);
});

xtest('Three digit number that is an Armstrong number', () => {
expect(isArmstrongNumber(153)).toEqual(true);
});

xtest('Three digit number that is not an Armstrong number', () => {
expect(isArmstrongNumber(100)).toEqual(false);
});

xtest('Four digit number that is an Armstrong number', () => {
expect(isArmstrongNumber(9474)).toEqual(true);
});

xtest('Four digit number that is not an Armstrong number', () => {
expect(isArmstrongNumber(9475)).toEqual(false);
});

xtest('Seven digit number that is an Armstrong number', () => {
expect(isArmstrongNumber(9926315)).toEqual(true);
});

xtest('Seven digit number that is not an Armstrong number', () => {
expect(isArmstrongNumber(9926314)).toEqual(false);
});
});``````
``````export const isArmstrongNumber = (number) => {
let power = number.toString().length;
let result = 0;
let digit = (number.toString().split(''));
for (let x = 0; x < power; x++) {
result = result + digit[x] ** power;
}

if( result == number){
return(true)
} else {
return(false)
}

};

// function isArmstrongNumber(number) {

// }

// console.log(isArmstrongNumber(9));
// console.log(isArmstrongNumber(10));
// console.log(isArmstrongNumber(153));
// console.log(isArmstrongNumber(154));``````

### 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?