|
| 1 | +// Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js |
| 2 | + |
1 | 3 | // This statement loads the isProperFraction function you wrote in the implement directory. |
2 | 4 | // We will use the same function, but write tests for it using Jest in this file. |
3 | 5 | const isProperFraction = require("../implement/2-is-proper-fraction"); |
@@ -26,3 +28,67 @@ test("should return true for a negative proper fraction (|numerator| < denominat |
26 | 28 | test("should return false when numerator equals denominator", () => { |
27 | 29 | expect(isProperFraction(3, 3)).toEqual(false); |
28 | 30 | }); |
| 31 | + |
| 32 | +// When the numerator is negative but |numerator| >= denominator, |
| 33 | +// Then the function should return false |
| 34 | +test("should return false for a negative improper fraction (|numerator| >= denominator)", () => { |
| 35 | + expect(isProperFraction(-8, 7)).toEqual(false); |
| 36 | +}); |
| 37 | + |
| 38 | +// When the numerator is zero, |
| 39 | +// Then the function should return true |
| 40 | +test("should return true when numerator is zero", () => { |
| 41 | + expect(isProperFraction(0, 5)).toEqual(true); |
| 42 | +}); |
| 43 | + |
| 44 | +// When the denominator is zero, |
| 45 | +// Then the function should throw an error |
| 46 | +test("should throw an error when denominator is zero", () => { |
| 47 | + expect(() => { |
| 48 | + isProperFraction(3, 0); |
| 49 | + }).toThrow("Denominator cannot be zero"); |
| 50 | +}); |
| 51 | + |
| 52 | +// When the numerator or denominator is not an integer, |
| 53 | +// Then the function should throw an error |
| 54 | +test("should throw an error when numerator or denominator is not an integer", () => { |
| 55 | + expect(() => { |
| 56 | + isProperFraction(2.5, 4); |
| 57 | + }).toThrow("Numerator and denominator must be integers"); |
| 58 | + |
| 59 | + expect(() => { |
| 60 | + isProperFraction(3, "5"); |
| 61 | + }).toThrow("Numerator and denominator must be integers"); |
| 62 | +}); |
| 63 | + |
| 64 | + |
| 65 | +// Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js |
| 66 | +// This function takes a card string (e.g., "A♠", "10♥", "K♦") and returns its value in a game of cards. |
| 67 | +function getCardValue(card) { |
| 68 | + // Extract rank by removing suit symbol (last character) |
| 69 | + const rank = card.slice(0, -1); |
| 70 | + |
| 71 | + // Map face cards to values |
| 72 | + const faceValues = { |
| 73 | + J: 10, |
| 74 | + Q: 10, |
| 75 | + K: 10, |
| 76 | + A: 11, |
| 77 | + }; |
| 78 | + |
| 79 | + // Return mapped value if rank is a face card |
| 80 | + if (faceValues[rank]) { |
| 81 | + return faceValues[rank]; |
| 82 | + } |
| 83 | + |
| 84 | + // Convert to number and validate numeric cards (2–10) |
| 85 | + const numericValue = Number(rank); |
| 86 | + if (!isNaN(numericValue) && numericValue >= 2 && numericValue <= 10) { |
| 87 | + return numericValue; |
| 88 | + } |
| 89 | + |
| 90 | + // Invalid card |
| 91 | + throw new Error("Invalid card rank"); |
| 92 | +} |
| 93 | + |
| 94 | +module.exports = getCardValue; |
0 commit comments