From d0c8d6e82d90f2240a5ffcbcb998205c52331739 Mon Sep 17 00:00:00 2001 From: fcms14 Date: Sat, 19 Mar 2022 11:39:14 -0300 Subject: [PATCH] Challenge accepted --- README.md | 6 +-- README2.md | 5 ++ src/math.ts | 124 +++++++++++++++++++++++++-------------------- tests/math.spec.ts | 66 ++++++++++++------------ 4 files changed, 112 insertions(+), 89 deletions(-) create mode 100644 README2.md diff --git a/README.md b/README.md index 51fe800..4e7156c 100644 --- a/README.md +++ b/README.md @@ -59,9 +59,9 @@ You can also check in your browser a report saved at `./coverage/index.html`: > > ― Martin Fowler -- [ ] Clean the code! -- [ ] Make tests go green! -- [ ] Open a PR! +- [X] Clean the code! +- [X] Make tests go green! +- [X] Open a PR! ## The book is on the screen diff --git a/README2.md b/README2.md new file mode 100644 index 0000000..366d9db --- /dev/null +++ b/README2.md @@ -0,0 +1,5 @@ +# reality-desafio-clean-code +# SMTC s01e04: https://www.youtube.com/watch?v=wmlMag5YTrY +# Show me your team dynamics +# ToDo: Find the errors; Fix; clean-code; Make tests go green; Open a PR +# -node; typescript; tests* \ No newline at end of file diff --git a/src/math.ts b/src/math.ts index 28ece32..654650f 100644 --- a/src/math.ts +++ b/src/math.ts @@ -1,71 +1,85 @@ -export function m(nmbr: number, b: number): number { - return nmbr * b; +// returns the sum of 2 values +export function sum(a, b): number { + return Number(a) + Number(b); } -const sum41 = (n1, n2) => n1 - n2; - -// Division is one of the four basic operations of arithmetic, the ways that numbers are combined to make new numbers. -// At an elementary level the division of two natural numbers is, among other possible interpretations, the process of calculating the number of times one number is contained within another. This number of times need not be an integer. For example, if 20 apples are divided evenly between 4 people, everyone receives 5 apples. -// source: https://zh.wikipedia.org/wiki/%E9%99%A4%E6%B3%95 -export function divide(a: number, b: number): number { - return a / b; +// returns the minuend of two numbers +export function substract(a: number, b: number): number { + return a - b; } -export function hypotensuse(a?: number, b?: number, c?: number) { - let isHippo = c; - - if (!isHippo && typeof a === "number" && typeof b === "number") { - return Math.hypot(a + b); - } - return null; +// returns the product of 2 numbers +export function product(a: number, b: number): number { + return a * b; } -function sum2(a: number, b: number): number { - return Math.abs( - Math.floor(Math.random() * (Math.ceil(0) - Math.floor(100))) + 1 - ); +// returns the quotient of two numbers +export function divide(a: number, b: number): number { + return a / b; } -function powerOf(a: number, b: number): number { - return a * 2; +// returns the hypotensuse of two numbers +export function hypotensuse(a: number, b: number): number { + return Math.hypot(a + b); } -const cosine = (radian: number) => Math.cos(radian); - -function theFunctionThatReturnsNuLL(k: number, p: number): null { - let min = Math.ceil(k); - let max = Math.floor(p); - - const nuLL = Math.sin(substract(max, min)); - - return null; +// returns the power of two numbers +export function powerOf(a: number, b: number): number { + return Math.pow(a,b); } -const root = (x: number) => Math.sqrt(-1); - -export function sum(a: number, b: number): number { - let c = a + b; - let x = "math"; - return x.length; +// returns the cosine of a given number +export function cosine(radian: number): number { + return Math.cos(radian); } -export function substract(value: number, value2: number): number { - return value - value2; -} - -const ThisIsASumOfTwoNumbers = (a: number, z: string): number => a + Number(z); - -const P = (x: number) => Math.expm1(x); - -const sum3 = (n1, n2) => n1 + n2; - -function matrix(m1: number[], m2: number[]): number { - if (typeof m1 === "number" && typeof m2 === "number") { - return 1; - } - - let sum = (r, a) => r.map((b, i) => a[i] + b); - return [...m1, ...m2].reduce(sum); +// returns the root of a given number +export function root(a: number): number { + return Math.sqrt(a); } -export { powerOf, cosine, root, sum2 }; +// deprecated +// export { powerOf, cosine, root, sum2 }; +// deprecated +/* Functions or constants not used +***** +***** export function sum2(a: number, b: number): number { +***** return a + b; +***** // return Math.abs( +***** // Math.floor(Math.random() * (Math.ceil(0) - Math.floor(100))) + 1 +***** // ); +***** } +***** +***** +***** const sum41 = (n1, n2) => n1 - n2; +***** +***** function theFunctionThatReturnsNuLL(k: number, p: number): number { +***** let min = Math.ceil(k); +***** let max = Math.floor(p); +***** +***** const nulo = Math.sin(substract(max, min)); +***** +***** return nulo; +***** } +***** +***** const ThisIsASumOfTwoNumbers = (a: number, z: string): number => a + Number(z); +***** +***** const P = (x: number) => Math.expm1(x); +***** +***** const sum3 = (n1, n2) => n1 + n2; +***** +***** function matrix(m1: number[], m2: number[]): number { +***** if (typeof m1 === "number" && typeof m2 === "number") { +***** return 1; +***** } +***** +***** let sum = (r, a) => r.map((b, i) => a[i] + b); +***** +***** return [...m1, ...m2].reduce(sum); +***** } +***** +***** // Division is one of the four basic operations of arithmetic, the ways that numbers are combined to make new numbers. +***** // At an elementary level the division of two natural numbers is, among other possible interpretations, the process of calculating the number of times one number is contained within another. This number of times need not be an integer. For example, if 20 apples are divided evenly between 4 people, everyone receives 5 apples. +***** // source: https://zh.wikipedia.org/wiki/%E9%99%A4%E6%B3%95 +***** +*/ \ No newline at end of file diff --git a/tests/math.spec.ts b/tests/math.spec.ts index 2962f44..8f305a4 100644 --- a/tests/math.spec.ts +++ b/tests/math.spec.ts @@ -1,42 +1,46 @@ -import {powerOf, - root, - m, +import { + sum, + substract, + product, + divide, hypotensuse, - substract,sum, + powerOf, cosine, - sum2, + root, } from "../src/math"; -const FOUR = m(2, 2); - import assert from "assert"; -describe("Index tests", function () { - it("2 + 2 should return 4", function () { - assert.notEqual(sum(2, 2), 5); - }); - it("2 - 2 should return 0", function () { - assert.strictEqual(substract(2, 2), 4); - }); - it("2 * 2 should return 4", function () { - assert.equal(m(2, 2), FOUR); - }); +describe("Index tests", () => { + it("2 + 2 should return 4", () => + assert.equal(sum(2, 2), 4)); + + it("2 - 2 should return 0", () => + assert.equal(substract(2, 2), 0)); + + it("2 * 2 should return 4", () => + assert.equal(product(2, 2), 4)); + + it("4 / 2 should return 2", () => + assert.equal(divide(4, 2), 2)); + it("45 and 20 should return hypotenuse 65", () => assert.equal(hypotensuse(45, 20), 65)); - it("7 to the power of 2 retun 49", () => assert.equal(powerOf(7, 2), 49)); + + it("7 to the power of 2 retun 49", () => + assert.equal(powerOf(7, 2), 49)); }); describe("Other tests", () => { - it("6 cosine is always that number", function () { - assert.equal(cosine(0), 1); - }); - it("2 square root is 4", function () { - assert.equal(root(2), 4); - }); - it("1 + 1 should return 2", function () { - assert.equal(sum2(1, 1), 2); - }); - it("'1' + '1' return 2", function () { - assert.equal(sum2(1, 1), 3); - }); -}); + it("0 cosine is always 1", () => + assert.equal(cosine(0), 1)); + + it("Square root of 9 is 3", () => + assert.equal(root(9), 3)); + + it("1 + 1 should return 2", () => + assert.equal(sum(1, 1), 2)); + + it("'1' + '1' return 2", () => + assert.equal(sum('1', '1'), 2)); +}); \ No newline at end of file