Skip to content

Commit 5cd25f5

Browse files
committed
Completed exercise 2-is-proper-fraction.js and 2-is-proper-fraction-test.js updated code and test
1 parent e8ca830 commit 5cd25f5

File tree

3 files changed

+50
-21
lines changed

3 files changed

+50
-21
lines changed

Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,14 @@
1111
// execute the code to ensure all tests pass.
1212

1313
function isProperFraction(numerator, denominator) {
14-
// Denominator cannot be 0
15-
if (denominator === 0)
16-
return false;
17-
if (!Number.isFinite(numerator) || !Number.isFinite(denominator)){
18-
return false;
19-
}
20-
if (!Number.isInteger(numerator) || !Number.isInteger(denominator)){
21-
return false;
22-
}
23-
//if is less than
24-
return Math.abs(numerator) < Math.abs(denominator);
25-
}
14+
// Denominator cannot be 0 because it won't make a fraction.
15+
if (denominator === 0) {
16+
return false;
17+
}
18+
// Compare absolute values to eliminate negative values
19+
//Proper fraction = absolute value of numerator < absolute value of denominator
20+
return Math.abs(numerator) < Math.abs(denominator);
21+
}
2622

2723
// The line below allows us to load the isProperFraction function into tests in other files.
2824
// This will be useful in the "rewrite tests with jest" step.
@@ -43,6 +39,4 @@ function assertEquals(actualOutput, targetOutput) {
4339
assertEquals(isProperFraction(1, 2), true);
4440
assertEquals(isProperFraction(2, 1), false);
4541
assertEquals(isProperFraction(-1, -2), true);
46-
assertEquals(isProperFraction("1", 2), false);
4742
assertEquals(isProperFraction(1, 0), false);
48-

Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,50 @@
2121
// After you have implemented the function, write tests to cover all the cases, and
2222
// execute the code to ensure all tests pass.
2323

24+
// TODO: Implement this function
2425
function getCardValue(card) {
25-
// TODO: Implement this function
26+
const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
27+
const validSuits = ["♠", "♥", "♦", "♣"];
28+
//Validation
29+
// Must be a string
30+
if (typeof card !== "string") {
31+
throw Error("Invalid card");
32+
}
33+
// Must be at least 2 characters and maximum of 3
34+
//
35+
//
2636
}
37+
// ---------- INVALID CHECKS FIRST ----------
2738

39+
// 1. Must be a string
40+
41+
42+
// 2. Must have at least 2 characters (rank + suit)
43+
if (card.length < 2) {
44+
throw new Error("Invalid card");
45+
}
46+
47+
// 3. Extract suit and rank
48+
const suit = card.slice(-1);
49+
const rank = card.slice(0, -1);
50+
51+
// 4. Suit must be valid
52+
if (!validSuits.includes(suit)) {
53+
throw new Error("Invalid card");
54+
}
55+
56+
// 5. Rank must be valid
57+
if (!validRanks.includes(rank)) {
58+
throw new Error("Invalid card");
59+
}
60+
61+
// ---------- ALL INPUT IS NOW VALID ----------
62+
63+
if (rank === "A") return 11;
64+
if (["J", "Q", "K"].includes(rank)) return 10;
65+
66+
return Number(rank);
67+
}
2868
// The line below allows us to load the getCardValue function into tests in other files.
2969
// This will be useful in the "rewrite tests with jest" step.
3070
module.exports = getCardValue;

Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,4 @@ test(`should return false when denominator is smaller than the numerator (1)`, (
1919

2020
test(`should return true when both values are valid numbers`, () => {
2121
expect(isProperFraction(-1, -2)).toEqual(true);
22-
});
23-
24-
test(`should return false when a number is not an integer`, () => {
25-
expect(isProperFraction("1", 2)).toEqual(false);
26-
});
27-
22+
});

0 commit comments

Comments
 (0)