tangled
alpha
login
or
join now
thecoded.prof
/
CMU
0
fork
atom
CMU Coding Bootcamp
0
fork
atom
overview
issues
pulls
pipelines
feat: oct 21
thecoded.prof
4 months ago
952376a8
b545761f
verified
This commit was signed with the committer's
known signature
.
thecoded.prof
SSH Key Fingerprint:
SHA256:ePn0u8NlJyz3J4Zl9MHOYW3f4XKoi5K1I4j53bwpG0U=
+43
4 changed files
expand all
collapse all
unified
split
ts
oct21
collatz.ts
evenDigits.ts
fizzBuzz.ts
index.test.ts
+6
ts/oct21/collatz.ts
···
1
1
+
export const collatz = (n: number): number[] => {
2
2
+
if (n <= 0) throw new Error("Input must be a positive integer");
3
3
+
if (n === 2) return [2, 1];
4
4
+
if (n % 2 === 0) return [n, ...collatz(n / 2)];
5
5
+
return [n, ...collatz(3 * n + 1)];
6
6
+
};
+8
ts/oct21/evenDigits.ts
···
1
1
+
export const evenDigits = (n: number): boolean => {
2
2
+
while (n > 0) {
3
3
+
const digit = n % 10;
4
4
+
if (digit % 2 !== 0) return false;
5
5
+
n = Math.floor(n / 10);
6
6
+
}
7
7
+
return true;
8
8
+
};
+6
ts/oct21/fizzBuzz.ts
···
1
1
+
export const fizzBuzz = (n: number): string => {
2
2
+
if (n % 3 === 0 && n % 5 === 0) return "fizzBuzz";
3
3
+
if (n % 3 === 0) return "fizz";
4
4
+
if (n % 5 === 0) return "buzz";
5
5
+
return n.toString();
6
6
+
};
+23
ts/oct21/index.test.ts
···
1
1
+
import { expect, test } from "bun:test";
2
2
+
3
3
+
import { fizzBuzz } from "./fizzBuzz";
4
4
+
import { collatz } from "./collatz";
5
5
+
import { evenDigits } from "./evenDigits";
6
6
+
7
7
+
test("FizzBuzz", () => {
8
8
+
expect(fizzBuzz(1)).toBe("1");
9
9
+
expect(fizzBuzz(3)).toBe("fizz");
10
10
+
expect(fizzBuzz(5)).toBe("buzz");
11
11
+
expect(fizzBuzz(15)).toBe("fizzBuzz");
12
12
+
});
13
13
+
14
14
+
test("Collatz", () => {
15
15
+
expect(collatz(3)).toEqual([3, 10, 5, 16, 8, 4, 2, 1]);
16
16
+
expect(collatz(1)).toEqual([1, 4, 2, 1]);
17
17
+
expect(collatz(4)).toEqual([4, 2, 1]);
18
18
+
});
19
19
+
20
20
+
test("All Even Digits", () => {
21
21
+
expect(evenDigits(2486)).toBe(true);
22
22
+
expect(evenDigits(1234)).toBe(false);
23
23
+
});