Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,20 @@

function getAngleType(angle) {
// TODO: Implement this function
if (angle > 0 && angle < 90) {
return "Acute angle";
}
if (angle === 90) return "Right angle";
if (angle > 90 && angle < 180) {
return "Obtuse angle";
}
if (angle === 180) return "Straight angle";
else if (angle > 180 && angle < 360) {
return "Reflex angle";
} else {
return "Invalid angle";
}
}

// The line below allows us to load the getAngleType function into tests in other files.
// This will be useful in the "rewrite tests with jest" step.
module.exports = getAngleType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (denominator <= 0) {
return false;
}
return numerator < denominator;
}

// The line below allows us to load the isProperFraction function into tests in other files.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,40 @@

function getCardValue(card) {
// TODO: Implement this function
const validSuits = ["♠", "♥", "♦", "♣"];
// Get the suit (last character) and rank (everything before it)
const suit = card.slice(-1);
const rank = card.slice(0, -1);
console.log(suit, rank);

// Check that the suit is valid
if (!validSuits.includes(suit)) {
throw new Error("Invalid card");
}
// Ace
if (rank === "A") {
return 11;
}

if (rank === "J" || rank === "Q" || rank === "K") {
return 10;
}

if (
rank === "2" ||
rank === "3" ||
rank === "4" ||
rank === "5" ||
rank === "6" ||
rank === "7" ||
rank === "8" ||
rank === "9" ||
rank === "10"
) {
return Number(rank);
}

throw new Error("Invalid card");
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -37,18 +71,30 @@ function assertEquals(actualOutput, targetOutput) {
);
}

// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:
assertEquals(getCardValue("9♠"), 9);
// // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// // Examples:
// assertEquals(getCardValue("9♠"), 9);

// Handling invalid cards
try {
getCardValue("invalid");
// // Handling invalid cards
// try {
// getCardValue("invalid");

// This line will not be reached if an error is thrown as expected
console.error("Error was not thrown for invalid card 😢");
} catch (e) {
console.log("Error thrown for invalid card 🎉");
}
// // This line will not be reached if an error is thrown as expected
// console.error("Error was not thrown for invalid card 😢");
// } catch (e) {
// console.log("Error thrown for invalid card 🎉");
// }

// // What other invalid card cases can you think of?
// //invalid rank
// getCardValue("1♠");
// getCardValue("11♣");
// getCardValue("0♦");
// getCardValue("B♥");
// getCardValue("X♠");

// What other invalid card cases can you think of?
// //invalid suit
// getCardValue("AX");
// getCardValue("5*");
// getCardValue("10H");
// getCardValue("QS");
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,29 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

// Case 2: Right angle
test(`should return "Right angle" when angle is 90`, () => {
expect(getAngleType(90)).toEqual("Right angle");
});
// Case 3: Obtuse angles
test(`should return "Obtuse angle" when angel is >90 & <180`, () => {
expect(getAngleType(100)).toEqual("Obtuse angle");
expect(getAngleType(95)).toEqual("Obtuse angle");
});
// Case 4: Straight angle
test(`should return "Straight angle" when angle === 180`, () => {
expect(getAngleType(180)).toEqual("Straight angle");
});
// Case 5: Reflex angles
test('should return "Reflex angle" when 180 < angle < 360', () => {
expect(getAngleType(181)).toEqual("Reflex angle");
expect(getAngleType(260)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test('should return "Invalid angle" for angles outside the valid range', () => {
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(-10)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(500)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
// This statement loads the isProperFraction function you wrote in the implement directory.
// We will use the same function, but write tests for it using Jest in this file.
const isProperFraction = require("../implement/2-is-proper-fraction");

// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.
test("should return true when numerator is less than denominator", () => {
expect(isProperFraction(1, 2)).toEqual(true);
expect(isProperFraction(3, 4)).toEqual(true);
});

test("should return false when numerator is greater than denominator", () => {
expect(isProperFraction(5, 2)).toEqual(false);
expect(isProperFraction(10, 3)).toEqual(false);
});

test("should return false when numerator equals denominator", () => {
expect(isProperFraction(4, 4)).toEqual(false);
});

test("should return true when numerator is zero and denominator is positive", () => {
expect(isProperFraction(0, 5)).toEqual(true);
});

// Special case: numerator is zero
test(`should return false when denominator is zero`, () => {
test("should return false when denominator is zero", () => {
expect(isProperFraction(1, 0)).toEqual(false);
});

test("should return false when numerator and denominator are zero", () => {
expect(isProperFraction(0, 0)).toEqual(false);
});

test("should return true when numerator is negative and denominator is positive", () => {
expect(isProperFraction(-1, 6)).toEqual(true);
});

test("should return false when denominator is negative", () => {
expect(isProperFraction(1, -5)).toEqual(false);
});

test("should return false when both numerator and denominator are negative", () => {
expect(isProperFraction(-4, -5)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,35 @@ const getCardValue = require("../implement/3-get-card-value");
test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
});
// Case 2: Number cards (2–10)
test("should return the value of number cards", () => {
expect(getCardValue("2♠")).toEqual(2);
expect(getCardValue("5♦")).toEqual(5);
expect(getCardValue("10♣")).toEqual(10);
});

// Case 3: Face cards (J, Q, K)
test("should return 10 for face cards", () => {
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("Q♥")).toEqual(10);
expect(getCardValue("K♦")).toEqual(10);
});

// Case 4: Invalid cards
test("should return error for invalid cards", () => {
expect(() => {
getCardValue("1♠");
}).toThrow("Invalid card");
expect(() => {
getCardValue("11♠");
}).toThrow("Invalid card");
expect(() => {
getCardValue("X♦");
}).toThrow("Invalid card");
expect(() => {
getCardValue("");
}).toThrow("Invalid card");
});

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
Expand All @@ -17,4 +46,3 @@ test(`Should return 11 when given an ace card`, () => {
// To learn how to test whether a function throws an error as expected in Jest,
// please refer to the Jest documentation:
// https://jestjs.io/docs/expect#tothrowerror

Loading