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,7 +16,20 @@

function getAngleType(angle) {
// TODO: Implement this function
}
if (angle > 0 && angle < 90) {
return "Acute angle";
} else if (angle === 90) {
return "Right angle";
} else if (angle > 90 && angle < 180) {
return "Obtuse angle";
} else 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.
Expand All @@ -35,3 +48,13 @@ function assertEquals(actualOutput, targetOutput) {
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");
const acute = getAngleType(45);
assertEquals(acute, "Acute angle");
const obtuse = getAngleType(120);
assertEquals(obtuse, "Obtuse angle");
const straight = getAngleType(180);
assertEquals(straight, "Straight angle");
const reflex = getAngleType(270);
assertEquals(reflex, "Reflex angle");
const invalid = getAngleType(400);
assertEquals(invalid, "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {


// TODO: Implement this function
if (denominator === 0) {
return false;
}
if (Math.abs(numerator) < Math.abs(denominator)) {
return true;
} else {
return false;
}
}

// The line below allows us to load the isProperFraction function into tests in other files.
Expand All @@ -24,10 +34,19 @@ function assertEquals(actualOutput, targetOutput) {
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);

}

// TODO: Write tests to cover all cases.
// What combinations of numerators and denominators should you test?

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(2, 1), false);
assertEquals(isProperFraction(-1, 2), true);
assertEquals(isProperFraction(1, -2), true);
assertEquals(isProperFraction(0, 2), true);
assertEquals(isProperFraction(2, 2), false);
assertEquals(isProperFraction(-2, -1), false);
assertEquals(isProperFraction(-2, -2), false);
assertEquals(isProperFraction(1, 0), false);
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@

function getCardValue(card) {
// TODO: Implement this function
const rank = card.slice(0, -1); // Get the rank by slicing off the last character (the suit)
const suit = card.slice(-1); // Get the suit by taking the last character of the string

// Validate the suit
const validSuits = ["♠", "♥", "♦", "♣"];
if (!validSuits.includes(suit)) {
throw new Error("Invalid card: Invalid suit");
}
// Determine the value based on the rank
if (rank === "A") {
return 11;
} else if (["J", "Q", "K"].includes(rank)) {
return 10;
} else if (!isNaN(rank) && parseInt(rank) >= 2 && parseInt(rank) <= 10) {
return parseInt(rank);
} else {
throw new Error("Invalid card: Invalid rank");
}
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -40,13 +58,28 @@ function assertEquals(actualOutput, targetOutput) {
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:
assertEquals(getCardValue("9♠"), 9);
assertEquals(getCardValue("A♥"), 11);
assertEquals(getCardValue("J♦"), 10);
assertEquals(getCardValue("Q♣"), 10);
assertEquals(getCardValue("K♦"), 10);
assertEquals(getCardValue("10♥"), 10);

// 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) {}
// Test invalid cards - these should throw errors
function testInvalidCard(card) {
try {
getCardValue(card);
console.error(`Error was not thrown for invalid card: ${card}`);
} catch (e) {
console.log(`✓ Correctly threw error for: ${card}`);
}
}

// What other invalid card cases can you think of?
testInvalidCard("11♠"); // Invalid rank
testInvalidCard("A"); // Missing suit
testInvalidCard("2X"); // Invalid suit
testInvalidCard("invalid"); // Completely invalid
testInvalidCard(""); // Empty string
testInvalidCard("A♠♠"); // Extra character
testInvalidCard("1♠"); // Invalid rank (1 instead of A)
testInvalidCard("B♠"); // Invalid rank
testInvalidCard("A♠A"); // Invalid format
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,41 @@ const getAngleType = require("../implement/1-get-angle-type");
// TODO: Write tests in Jest syntax to cover all cases/outcomes,
// including boundary and invalid cases.


// Case 1: Acute angles
test(`should return "Acute angle" when (0 < angle < 90)`, () => {
// Test various acute angles, including boundary cases
test("should identify acute angles", () => {
expect(getAngleType(1)).toEqual("Acute angle");
expect(getAngleType(45)).toEqual("Acute angle");
expect(getAngleType(89)).toEqual("Acute angle");
});

// Case 2: Right angle
test("should identify right angles", () => {
expect(getAngleType(90)).toEqual("Right angle");
});

// Case 3: Obtuse angles
test("should identify obtuse angles", () => {
expect(getAngleType(91)).toEqual("Obtuse angle");
expect(getAngleType(120)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
});

// Case 4: Straight angle
test("should identify straight angles", () => {
expect(getAngleType(180)).toEqual("Straight angle");
});

// Case 5: Reflex angles
test("should identify reflex angles", () => {
expect(getAngleType(190)).toEqual("Reflex angle");
expect(getAngleType(270)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test("should identify invalid angles", () => {
expect(getAngleType(-1)).toEqual("Invalid angle");
expect(getAngleType(361)).toEqual("Invalid angle");
expect(getAngleType(400)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,27 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});
// Case 1: Proper fractions
test(`should return true for proper fractions`, () => {
expect(isProperFraction(1, 2)).toEqual(true);
expect(isProperFraction(-1, 2)).toEqual(true);
expect(isProperFraction(1, -2)).toEqual(true);
expect(isProperFraction(-1, -2)).toEqual(true);
expect(isProperFraction(0, 2)).toEqual(true);
});

// Case 2: Improper fractions
test(`should return false for improper fractions`, () => {
expect(isProperFraction(2, 1)).toEqual(false);
expect(isProperFraction(-2, 1)).toEqual(false);
expect(isProperFraction(2, -1)).toEqual(false);
expect(isProperFraction(-2, -1)).toEqual(false);
expect(isProperFraction(2, 2)).toEqual(false);
expect(isProperFraction(-2, -2)).toEqual(false);
});
// Case 3: Invalid fractions (denominator is zero)
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
expect(isProperFraction(-1, 0)).toEqual(false);
expect(isProperFraction(0, 0)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,19 @@ test(`Should return 11 when given an ace card`, () => {

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
test(`Should return 2 when given a 2 card`, () => {
expect(getCardValue("2♠")).toEqual(2);
});
test(`Should return 10 when given a 10 card`, () => {
expect(getCardValue("10♠")).toEqual(10);
});
// Face Cards (J, Q, K)
// Invalid Cards

test(`Should throw an error when given an invalid card`, () => {
expect(() => getCardValue("11♠")).toThrow("Invalid card: Invalid rank");
expect(() => getCardValue("A")).toThrow("Invalid card: Invalid suit");
expect(() => getCardValue("2X")).toThrow("Invalid card: Invalid suit");
});
// 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