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 @@ -15,13 +15,27 @@
// execute the code to ensure all tests pass.

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";
}
if (angle > 180 && angle < 360) {
return "Reflex angle";
}
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;

// This helper function is written to make our assertions easier to read.
// If the actual output matches the target output, the test will pass
function assertEquals(actualOutput, targetOutput) {
Expand All @@ -33,5 +47,15 @@ function assertEquals(actualOutput, targetOutput) {

// TODO: Write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles
const acute = getAngleType(45);
assertEquals(acute, "Acute angle");
const right = getAngleType(90);
assertEquals(right, "Right angle");
const obtuse = getAngleType(135);
assertEquals(obtuse, "Obtuse angle");
const straight = getAngleType(180);
assertEquals(straight, "Straight angle");
const reflex = getAngleType(270);
assertEquals(reflex, "Reflex angle");
const invalidAngle = getAngleType(-10);
assertEquals(invalidAngle, "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (numerator === 0) {
return false;
}
if (denominator === 0) {
return false;
}
if (Number.isNaN(numerator) || Number.isNaN(denominator)) {
return false;
}
return Math.abs(numerator) < Math.abs(denominator);
}

// The line below allows us to load the isProperFraction function into tests in other files.
Expand All @@ -25,6 +34,25 @@ function assertEquals(actualOutput, targetOutput) {
`Expected ${actualOutput} to equal ${targetOutput}`
);
}
// Proper fractions
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(3, 4), true);
assertEquals(isProperFraction(-1, 5), true);
assertEquals(isProperFraction(1, -5), true);

// Improper fractions
assertEquals(isProperFraction(5, 3), false);
assertEquals(isProperFraction(10, 10), false);
assertEquals(isProperFraction(-7, 3), false);

// Zero cases
assertEquals(isProperFraction(0, 5), false);
assertEquals(isProperFraction(3, 0), false);

// NaN cases
assertEquals(isProperFraction(NaN, 5), false);
assertEquals(isProperFraction(3, NaN), false);
assertEquals(isProperFraction(NaN, NaN), false);

// TODO: Write tests to cover all cases.
// What combinations of numerators and denominators should you test?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,34 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
const ranks = [
"A",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"J",
"Q",
"K",
];
const suits = ["♠", "♥", "♦", "♣"];

let rank = card.slice(0, -1); // extracting the rank from the input
let suit = card.slice(-1); // extracting the suit from the input
if (!suits.includes(suit)) {
throw new Error("Invalid suit");
}
if (!ranks.includes(rank)) {
throw new Error("Invalid rank");
}
if (rank === "A") return 11;
if (["J", "Q", "K"].includes(rank)) return 10;
return Number(rank);
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -39,7 +66,19 @@ function assertEquals(actualOutput, targetOutput) {

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

// Face cards
assertEquals(getCardValue("J♣"), 10);
assertEquals(getCardValue("Q♦"), 10);
assertEquals(getCardValue("K♥"), 10);

// Ace
assertEquals(getCardValue("A♠"), 11);

// Handling invalid cards
try {
Expand All @@ -52,3 +91,30 @@ try {
}

// What other invalid card cases can you think of?
try {
getCardValue("");
console.error("Error NOT thrown for empty string");
} catch (e) {
console.log("Error thrown for empty string 🎉");
}

try {
getCardValue("1♠");
console.error("Error NOT thrown for invalid rank");
} catch (e) {
console.log("Error thrown for invalid rank 🎉");
}

try {
getCardValue("A");
console.error("Error NOT thrown for missing suit");
} catch (e) {
console.log("Error thrown for missing suit 🎉");
}

try {
getCardValue("A?");
console.error("Error NOT thrown for invalid suit");
} catch (e) {
console.log("Error thrown for invalid suit 🎉");
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,33 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

// Case 2: Right angle
test(`should return "Right angle" when angle === 90`, () => {
expect(getAngleType(90)).toEqual("Right angle");
});

// Case 3: Obtuse angles
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
expect(getAngleType(91)).toEqual("Obtuse angle");
expect(getAngleType(120)).toEqual("Obtuse angle");
expect(getAngleType(179)).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(250)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test(`should return "Invalid angle" for angles outside 0–360`, () => {
expect(getAngleType(0)).toEqual("Invalid angle"); // boundary
expect(getAngleType(360)).toEqual("Invalid angle"); // boundary
expect(getAngleType(-10)).toEqual("Invalid angle");
expect(getAngleType(400)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,40 @@ 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.

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

//Special case: numerator is zero
test(`should return false when numerator is zero`, () => {
expect(isProperFraction(0, 1)).toEqual(false);
});
// Proper functions with absolute numerator < absolute denominator
test(`should return true for proper positive fractions`, () => {
expect(isProperFraction(1, 2)).toEqual(true);
expect(isProperFraction(3, 4)).toEqual(true);
});
// Proper functions with negative numbers
test(`should return true for proper negative fractions`, () => {
expect(isProperFraction(-1, 2)).toEqual(true);
expect(isProperFraction(1, -3)).toEqual(true);
expect(isProperFraction(-2, -5)).toEqual(true);
});
// Improper functions with absolute numerator >= absolute denominator
test(`should return false for improper fractions`, () => {
expect(isProperFraction(5, 3)).toEqual(false);
expect(isProperFraction(10, 10)).toEqual(false);
expect(isProperFraction(-7, 3)).toEqual(false);
});
// Special cases: numerator or denominator is NAN
test(`should return false when numerator or denominator is NaN`, () => {
expect(isProperFraction(NaN, 5)).toEqual(false);
expect(isProperFraction(3, NaN)).toEqual(false);
expect(isProperFraction(NaN, NaN)).toEqual(false);
});
// Special cases: non-number strings
test(`should return false for non-numeric strings`, () => {
expect(isProperFraction("a", 5)).toEqual(false);
expect(isProperFraction(3, "b")).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,38 @@ test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
});

// Case 2: Number Cards (2-10)
test(`Should return correct values for number cards`, () => {
expect(getCardValue("2♠")).toEqual(2);
expect(getCardValue("7♥")).toEqual(7);
expect(getCardValue("9♦")).toEqual(9);
expect(getCardValue("10♣")).toEqual(10);
});

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

// Case 4: Invalid cards
test(`Should throw an error for invalid rank`, () => {
expect(() => getCardValue("1♠")).toThrowError("Invalid rank");
});

test(`Should throw an error for invalid suit`, () => {
expect(() => getCardValue("A?")).toThrowError("Invalid suit");
});

test(`Should throw an error for missing suit`, () => {
expect(() => getCardValue("A")).toThrowError();
});

test(`Should throw an error for empty string`, () => {
expect(() => getCardValue("")).toThrowError();
});

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
// Face Cards (J, Q, K)
Expand All @@ -17,4 +49,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