Skip to content

Commit 6c94102

Browse files
committed
🎉 Begin
0 parents  commit 6c94102

File tree

9 files changed

+135
-0
lines changed

9 files changed

+135
-0
lines changed

bean_counting.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {
2+
counChar,
3+
countBs,
4+
counterOcurrencesCreator,
5+
} from "./bean_counting.ts";
6+
import { assert } from "./testing.ts";
7+
8+
Deno.test("Main", () => {});

bean_counting.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function counChar(text: string, charToSearch: string) {
2+
const occurences = [...text].filter((letter) => letter === charToSearch);
3+
return occurences.length;
4+
}
5+
6+
export function counterOcurrencesCreator(charToSearch: string) {
7+
return (text: string) => counChar(text, charToSearch);
8+
}
9+
10+
export const countBs = counterOcurrencesCreator("B");

is_even.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { isEven } from "./is_even.ts";
2+
import { assert } from "./testing.ts";
3+
4+
Deno.test("Main", () => {
5+
assert(isEven(2));
6+
assert(!isEven(11));
7+
assert(isEven(24));
8+
assert(isEven(24));
9+
assert(isEven(50));
10+
assert(!isEven(75));
11+
});

is_even.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export function isEven(numberToTest: number): boolean {
2+
if (!Number.isInteger(numberToTest) || numberToTest <= -1) {
3+
throw new Error("Only positive numbers are allowed.");
4+
}
5+
6+
if (numberToTest === 0) {
7+
return true;
8+
}
9+
10+
if (numberToTest == 1) {
11+
return false;
12+
}
13+
14+
return isEven(numberToTest - 2);
15+
}

license

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Eliaz Bobadilla <eliaz.bobadilla@gmail.com> (https://ultirequiem.com)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

minimum.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { min, minRecursive } from "./minimum.ts";
2+
3+
import { assert } from "./testing.ts";
4+
5+
Deno.test("main", () => {
6+
assert(min(5, 3, 4, 2, 22) === 2);
7+
8+
assert(min(5, 3, 4, 2, 22, -3) === -3);
9+
assert(min(-23, 5, 3, 4, 2, 22, -3) === -23);
10+
});
11+
12+
Deno.test("recursive", () => {
13+
assert(minRecursive(5, 3, 4, 2, 22) === 2);
14+
15+
assert(minRecursive(5, 3, 4, 2, 22, -3) === -3);
16+
});

minimum.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// I prefer iterating over recursivity in almost all cases.
2+
// Is a good exercise to do some things with recursivity tho.
3+
4+
export function min(...numbers: number[]) {
5+
let smaller = Infinity;
6+
7+
for (const number of numbers) {
8+
if (number < smaller) {
9+
smaller = number;
10+
}
11+
}
12+
13+
return smaller;
14+
}
15+
16+
export function minRecursive(...numbers: number[]) {
17+
function finder(current: number, competitor?: number): number {
18+
if (!competitor) {
19+
return current;
20+
}
21+
22+
if (competitor < current) {
23+
current = competitor;
24+
}
25+
26+
return finder(current, numbers.pop());
27+
}
28+
29+
return finder(Infinity, numbers.pop());
30+
}

readme.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Eloquent JavaScript
2+
3+
My internet just gone and I just found I had Eloquent JavaScript downloaded sooo
4+
5+
I will update the test to use std/testing.ts as soon my internet is back.
6+
7+
## Test Driven Development
8+
9+
I hate test driven development.
10+
11+
> Not really, is cool sometimes.
12+
13+
The book doesn't talk about testing (Or I don't get to that part yet) but I
14+
implemented tests for every challenge and followed the TDD method.
15+
16+
> Write tests first, and write code that passes them.
17+
18+
## License
19+
20+
Licensed under the MIT License.

testing.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function assert(condition: unknown): asserts condition is true {
2+
if (!condition) {
3+
throw new Error("Codition is false!");
4+
}
5+
}

0 commit comments

Comments
 (0)