Skip to content

Commit 7f92d09

Browse files
committed
first commit
0 parents  commit 7f92d09

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# js-challenges
2+
# js-challenges

challenges/collatz/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// --- Directions
2+
// Generate the Collatz sequence starting with the positive integer n.
3+
// Start with any positive integer. If it's even, halve it; if odd, triple it and add 1.
4+
//// Repeat until you reach 1.
5+
// Example:
6+
// collatz(12) // Sequence: [12, 6, 3, 10, 5, 16, 8, 4, 2, 1]
7+
8+
function collatz(n) {}
9+
10+
module.exports = collatz;

challenges/collatz/test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const collatz = require('./index');
2+
3+
describe('Collatz Conjecture Challenge', () => {
4+
it('should return the correct sequence for a positive integer greater than 1', () => {
5+
const testCases = [
6+
{ input: 12, expectedSequence: [12, 6, 3, 10, 5, 16, 8, 4, 2, 1] },
7+
{ input: 7, expectedSequence: [7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1] },
8+
];
9+
10+
testCases.forEach(({ input, expectedSequence }) => {
11+
const result = collatz(input);
12+
expect(result).toEqual(expectedSequence);
13+
});
14+
});
15+
16+
it('should return [1] for input 1', () => {
17+
expect(collatz(1)).toEqual([1]);
18+
});
19+
20+
it('should return an empty array for input 0', () => {
21+
expect(collatz(0)).toEqual([]);
22+
});
23+
24+
it('should return an empty array for no input value', () => {
25+
expect(collatz(undefined)).toEqual([]);
26+
});
27+
});

package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "js-challenges",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "Matt Fay",
11+
"license": "ISC"
12+
}

0 commit comments

Comments
 (0)