Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ce6856a

Browse files
author
programmiri
committed
Add exercise Collatz Conjecture
1 parent f483369 commit ce6856a

File tree

6 files changed

+4370
-0
lines changed

6 files changed

+4370
-0
lines changed

‎collatz-conjecture/.eslintrc‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"root": true,
3+
"parser": "babel-eslint",
4+
"parserOptions": {
5+
"ecmaVersion": 7,
6+
"sourceType": "module"
7+
},
8+
"env": {
9+
"es6": true,
10+
"node": true,
11+
"jest": true
12+
},
13+
"extends": [
14+
"eslint:recommended",
15+
"plugin:import/errors",
16+
"plugin:import/warnings"
17+
],
18+
"rules": {
19+
"linebreak-style": "off",
20+
21+
"import/extensions": "off",
22+
"import/no-default-export": "off",
23+
"import/no-unresolved": "off",
24+
"import/prefer-default-export": "off"
25+
}
26+
}

‎collatz-conjecture/README.md‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Collatz Conjecture
2+
3+
The Collatz Conjecture or 3x+1 problem can be summarized as follows:
4+
5+
Take any positive integer n. If n is even, divide n by 2 to get n / 2. If n is
6+
odd, multiply n by 3 and add 1 to get 3n + 1. Repeat the process indefinitely.
7+
The conjecture states that no matter which number you start with, you will
8+
always reach 1 eventually.
9+
10+
Given a number n, return the number of steps required to reach 1.
11+
12+
## Examples
13+
14+
Starting with n = 12, the steps would be as follows:
15+
16+
0. 12
17+
1. 6
18+
2. 3
19+
3. 10
20+
4. 5
21+
5. 16
22+
6. 8
23+
7. 4
24+
8. 2
25+
9. 1
26+
27+
Resulting in 9 steps. So for input n = 12, the return value would be 9.
28+
29+
## Setup
30+
31+
Go through the setup instructions for Javascript to install the necessary
32+
dependencies:
33+
34+
[https://exercism.io/tracks/javascript/installation](https://exercism.io/tracks/javascript/installation)
35+
36+
## Requirements
37+
38+
Install assignment dependencies:
39+
40+
```bash
41+
$ npm install
42+
```
43+
44+
## Making the test suite pass
45+
46+
Execute the tests with:
47+
48+
```bash
49+
$ npm test
50+
```
51+
52+
In the test suites all tests but the first have been skipped.
53+
54+
Once you get a test passing, you can enable the next one by changing `xtest` to
55+
`test`.
56+
57+
## Source
58+
59+
An unsolved problem in mathematics named after mathematician Lothar Collatz [https://en.wikipedia.org/wiki/3x_%2B_1_problem](https://en.wikipedia.org/wiki/3x_%2B_1_problem)
60+
61+
## Submitting Incomplete Solutions
62+
63+
It's possible to submit an incomplete solution so you can see how others have
64+
completed the exercise.

‎collatz-conjecture/babel.config.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
presets: [
3+
[
4+
'@babel/env',
5+
{
6+
targets: {
7+
node: 'current',
8+
},
9+
useBuiltIns: false,
10+
},
11+
12+
],
13+
],
14+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { steps } from './collatz-conjecture';
2+
3+
describe('steps()', () => {
4+
test('zero steps for one', () => {
5+
expect(steps(1)).toEqual(0);
6+
});
7+
8+
xtest('divide if even', () => {
9+
expect(steps(16)).toEqual(4);
10+
});
11+
12+
xtest('even and odd steps', () => {
13+
expect(steps(12)).toEqual(9);
14+
});
15+
16+
xtest('Large number of even and odd steps', () => {
17+
expect(steps(1000000)).toEqual(152);
18+
});
19+
20+
xtest('zero is an error', () => {
21+
expect(() => {
22+
steps(0);
23+
}).toThrow(new Error('Only positive numbers are allowed'));
24+
});
25+
26+
xtest('negative value is an error', () => {
27+
expect(() => {
28+
steps(-15);
29+
}).toThrow(new Error('Only positive numbers are allowed'));
30+
});
31+
});

‎collatz-conjecture/package.json‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "exercism-javascript",
3+
"version": "0.0.0",
4+
"description": "Exercism exercises in Javascript.",
5+
"author": "Katrina Owen",
6+
"private": true,
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/exercism/javascript"
10+
},
11+
"devDependencies": {
12+
"@babel/cli": "^7.2.3",
13+
"@babel/core": "^7.4.0",
14+
"@babel/preset-env": "^7.4.2",
15+
"babel-eslint": "^10.0.1",
16+
"babel-jest": "^24.5.0",
17+
"eslint": "^5.15.3",
18+
"eslint-plugin-import": "^2.16.0",
19+
"jest": "^24.5.0"
20+
},
21+
"jest": {
22+
"modulePathIgnorePatterns": [
23+
"package.json"
24+
]
25+
},
26+
"scripts": {
27+
"test": "jest --no-cache ./*",
28+
"watch": "jest --no-cache --watch ./*",
29+
"lint": "eslint .",
30+
"lint-test": "eslint . && jest --no-cache ./* "
31+
},
32+
"license": "MIT",
33+
"dependencies": {}
34+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /