Skip to content

Commit 30cdea5

Browse files
committed
solved: reto mouredev#41
1 parent b626064 commit 30cdea5

File tree

3 files changed

+71
-14
lines changed

3 files changed

+71
-14
lines changed
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
node_modules
1+
node_modules
2+
package.json
3+
package-lock.json

app/src/main/java/com/mouredev/weeklychallenge2022/Challenge41.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,25 @@
1818
*/
1919

2020
export function ohm(input) {
21-
const errorMsg = "Invalid values";
22-
if (typeof input !== "object" || Array.isArray(input)) return errorMsg;
23-
else if (Object.keys(input).length !== 2) return errorMsg;
21+
// input validation
22+
const invalid =
23+
typeof input !== "object" ||
24+
Array.isArray(input) ||
25+
Object.keys(input).length !== 2 ||
26+
Object.keys(input).filter((key) => !["v", "r", "i"].includes(key)).length ||
27+
Object.values(input).filter(
28+
(value) => typeof value !== "number" || isNaN(value)
29+
).length;
30+
31+
if (invalid) return "Invalid values";
32+
33+
// calculation
34+
const { v, r, i } = input;
35+
const rounded = (num) => Math.round(num * 100) / 100;
36+
37+
return v === undefined
38+
? rounded(r * i)
39+
: r === undefined
40+
? rounded(v / i)
41+
: rounded(v / r);
2442
}
Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,62 @@
11
import { describe, it, expect } from "vitest";
22
import { ohm } from "./Challenge41";
33

4+
const errorMsg = "Invalid values";
5+
46
describe("Reto #41 - LA LEY DE OHM", () => {
57
it("ohm is a function", () => {
68
expect(ohm).toBeTypeOf("function");
79
});
810

911
it("returns message 'Invalid values' if the provided parameter is not an object", () => {
10-
expect(ohm({ v: 12, r: 220 })).not.toBe("Invalid values");
11-
expect(ohm()).toBe("Invalid values");
12-
expect(ohm(1, 2)).toBe("Invalid values");
13-
expect(ohm([1, 2])).toBe("Invalid values");
14-
expect(ohm("1, 2")).toBe("Invalid values");
12+
expect(ohm({ v: 12, r: 220 })).not.toBe(errorMsg);
13+
expect(ohm()).toBe(errorMsg);
14+
expect(ohm(1, 2)).toBe(errorMsg);
15+
expect(ohm([1, 2])).toBe(errorMsg);
16+
expect(ohm("1, 2")).toBe(errorMsg);
1517
});
1618

1719
it("returns message 'Invalid values' if the provided object has not two and only two properties", () => {
18-
expect(ohm({ v: 12 })).toBe("Invalid values");
19-
expect(ohm({ v: 12, r: 220 })).not.toBe("Invalid values");
20-
expect(ohm({ v: 12, r: 220, i: 0.05 })).toBe("Invalid values");
21-
expect(ohm({ v: 12, r: 220, i: 0.05, p: 0.82 })).toBe("Invalid values");
20+
expect(ohm({ v: 12 })).toBe(errorMsg);
21+
expect(ohm({ v: 12, r: 220 })).not.toBe(errorMsg);
22+
expect(ohm({ v: 12, r: 220, i: 0.05 })).toBe(errorMsg);
23+
expect(ohm({ v: 12, r: 220, i: 0.05, p: 0.82 })).toBe(errorMsg);
24+
});
25+
26+
it("returns message 'Invalid values' if any of the keys of the provided objects is not recognized", () => {
27+
expect(ohm({ v: 12, r: 220 })).not.toBe(errorMsg);
28+
expect(ohm({ r: 220, i: 0.05 })).not.toBe(errorMsg);
29+
expect(ohm({ v: 12, i: 0.05 })).not.toBe(errorMsg);
30+
expect(ohm({ i: 0.05, v: 12 })).not.toBe(errorMsg);
31+
expect(ohm({ v: 12, x: 220 })).toBe(errorMsg);
32+
expect(ohm({ volt: 12, resistance: 220 })).toBe(errorMsg);
33+
});
34+
35+
it("returns message 'Invalid values' if any of the values of the provided objects is not a number", () => {
36+
expect(ohm({ v: 12, r: 220 })).not.toBe(errorMsg);
37+
expect(ohm({ v: "1", r: 220 })).toBe(errorMsg);
38+
expect(ohm({ v: 12, r: true })).toBe(errorMsg);
39+
expect(ohm({ v: [1], r: 220 })).toBe(errorMsg);
40+
expect(ohm({ v: 12, r: { value: 220 } })).toBe(errorMsg);
41+
expect(ohm({ v: null, r: 220 })).toBe(errorMsg);
42+
expect(ohm({ v: 12, r: NaN })).toBe(errorMsg);
2243
});
2344

24-
it.skip("returns message 'Invalid values' if the object provided has not the required shape", () => {});
45+
it("returns a number", () => {
46+
expect(ohm({ v: 12, r: 220 })).toBeTypeOf("number");
47+
});
48+
49+
it("returns the result of the ohm's law", () => {
50+
expect(ohm({ v: 12, r: 120 })).toBe(0.1);
51+
expect(ohm({ v: 12, i: 0.1 })).toBe(120);
52+
expect(ohm({ i: 0.1, r: 120 })).toBe(12);
53+
});
54+
55+
it("the result is rounded to two decimal digits", () => {
56+
expect(ohm({ v: 12, r: 120 })).toBe(0.1);
57+
expect(ohm({ v: 12, r: 220 })).toBe(0.05);
58+
expect(ohm({ v: 12, r: 220 })).not.toBe(12 / 220);
59+
expect(ohm({ v: 12, i: 5.35 })).toBe(2.24);
60+
expect(ohm({ v: 12, i: 5.35 })).not.toBe(12 / 5.35);
61+
});
2562
});

0 commit comments

Comments
 (0)