Skip to content

Commit

Permalink
JS armstrong numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandleth committed Mar 10, 2019
1 parent b5e040b commit e334232
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 0 deletions.
1 change: 1 addition & 0 deletions javascript/armstrong-numbers/.exercism/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"track":"javascript","exercise":"armstrong-numbers","id":"a0217a4d961c48c2beba064ccea0386d","url":"https://exercism.io/my/solutions/a0217a4d961c48c2beba064ccea0386d","handle":"rolandleth","is_requester":true,"auto_approve":false}
48 changes: 48 additions & 0 deletions javascript/armstrong-numbers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Armstrong Numbers

An [Armstrong number](https://en.wikipedia.org/wiki/Narcissistic_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](https://exercism.io/tracks/javascript/installation)

## Requirements

Install assignment dependencies:

```bash
$ npm install
```

## Making the test suite pass

Execute the tests with:

```bash
$ 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 [https://en.wikipedia.org/wiki/Narcissistic_number](https://en.wikipedia.org/wiki/Narcissistic_number)

## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
18 changes: 18 additions & 0 deletions javascript/armstrong-numbers/armstrong-numbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const validate = (number) => {
if (number < 10) {
return true
}

if (number < 100) {
return false
}

const n = `${number}`
const result = n.split("").reduce((prev, current) => {
const value = parseInt(current, 10)

return prev + value ** n.length
}, 0)

return result == number
}
43 changes: 43 additions & 0 deletions javascript/armstrong-numbers/armstrong-numbers.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { validate } from "./armstrong-numbers"

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

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

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

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

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

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

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

test("Seven digit number that is not an Armstrong number", () => {
const input = 9926314
expect(validate(input)).toBe(false)
})
})
80 changes: 80 additions & 0 deletions javascript/armstrong-numbers/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
{
"name": "exercism-javascript",
"version": "0.0.0",
"description": "Exercism exercises in Javascript.",
"author": "Katrina Owen",
"private": true,
"repository": {
"type": "git",
"url": "https://github.com/exercism/javascript"
},
"devDependencies": {
"babel-eslint": "^10.0.1",
"babel-jest": "^23.6.0",
"babel-plugin-transform-builtin-extend": "^1.1.2",
"babel-preset-env": "^1.7.0",
"eslint": "^5.12.1",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.15.0",
"jest": "^23.6.0"
},
"jest": {
"modulePathIgnorePatterns": [
"package.json"
]
},
"babel": {
"presets": [
[
"env",
{
"targets": [
{
"node": "current"
}
]
}
]
],
"plugins": [
[
"babel-plugin-transform-builtin-extend",
{
"globals": [
"Error"
]
}
],
[
"transform-regenerator"
]
]
},
"scripts": {
"test": "jest --no-cache ./*",
"watch": "jest --no-cache --watch ./*",
"lint": "eslint .",
"lint-test": "eslint . && jest --no-cache ./* "
},
"eslintConfig": {
"parser": "babel-eslint",
"parserOptions": {
"ecmaVersion": 7,
"sourceType": "module"
},
"env": {
"es6": true,
"node": true,
"jest": true
},
"extends": "airbnb-base",
"rules": {
"import/no-unresolved": "off",
"import/extensions": "off",
"import/prefer-default-export": "off",
"import/no-default-export": "off"
}
},
"license": "MIT",
"dependencies": {}
}

0 comments on commit e334232

Please sign in to comment.