Avatar of brunnock

brunnock's solution

to Armstrong Numbers in the JavaScript Track

Published at Aug 25 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

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 = 2
  • 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 ECMAScript to install the necessary dependencies:

http://exercism.io/languages/ecmascript

Requirements

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.

Source

Wikipedia Narcissistic number

Submitting Incomplete Solutions

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

armstrong-numbers.spec.js

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

describe('ArmstrongNumber', () => {
  test('Single digit numbers are Armstrong numbers', () => {
    const input = 5;
    expect(validate(input)).toBe(true);
  });

  xtest('There are no 2 digit Armstrong numbers', () => {
    const input = 10;
    expect(validate(input)).toBe(false);
  });

  xtest('Three digit number that is an Armstrong number', () => {
    const input = 153;
    expect(validate(input)).toBe(true);
  });

  xtest('Three digit number that is not an Armstrong number', () => {
    const input = 100;
    expect(validate(input)).toBe(false);
  });

  xtest('Four digit number that is an Armstrong number', () => {
    const input = 9474;
    expect(validate(input)).toBe(true);
  });

  xtest('Four digit number that is not an Armstrong number', () => {
    const input = 9475;
    expect(validate(input)).toBe(false);
  });

  xtest('Seven digit number that is an Armstrong number', () => {
    const input = 9926315;
    expect(validate(input)).toBe(true);
  });

  xtest('Seven digit number that is not an Armstrong number', () => {
    const input = 9926314;
    expect(validate(input)).toBe(false);
  });
});
function validate(input) {
  let numArr = input.toString().split('');
  return numArr.reduce((sum,x)=>sum+=Number(x)**numArr.length,0)===input;
}

module.exports={validate};

Community comments

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

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?