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

function getAngleType(angle) {
// TODO: Implement this function
if (angle <= 0 || angle >= 360) {
return "Invalid angle";
}

if (angle === 90) {
return "Right angle";
}

if (angle > 0 && angle < 90) {
return "Acute angle";
}

if (angle > 90 && angle < 180) {
return "Obtuse angle";
}

if (angle === 180) {
return "Straight angle";
}

return "Reflex angle";
}

// The line below allows us to load the getAngleType function into tests in other files.
Expand All @@ -33,5 +53,29 @@ function assertEquals(actualOutput, targetOutput) {

// TODO: Write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");
// Acute Angles:
assertEquals(getAngleType(1), "Acute angle");
assertEquals(getAngleType(45), "Acute angle");
assertEquals(getAngleType(89), "Acute angle");

//Right Angles:
assertEquals(getAngleType(90), "Right angle");

//Obtuse Angles:
assertEquals(getAngleType(91), "Obtuse angle");
assertEquals(getAngleType(120), "Obtuse angle");
assertEquals(getAngleType(179), "Obtuse angle");

//Straight Angles:
assertEquals(getAngleType(180), "Straight angle");

//Reflex Angles:
assertEquals(getAngleType(181), "Reflex angle");
assertEquals(getAngleType(270), "Reflex angle");
assertEquals(getAngleType(359), "Reflex angle");

// Invalid Angles
assertEquals(getAngleType(0), "Invalid angle");
assertEquals(getAngleType(360), "Invalid angle");
assertEquals(getAngleType(-10), "Invalid angle");
assertEquals(getAngleType(720), "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (denominator === 0) 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 @@ -30,4 +31,28 @@ function assertEquals(actualOutput, targetOutput) {
// What combinations of numerators and denominators should you test?

// Example: 1/2 is a proper fraction

//Proper Fractions
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(2, 5), true);

//Equal values (Not proper fractions)
assertEquals(isProperFraction(2, 2), false);

//Improper Fractions
assertEquals(isProperFraction(5, 3), false);

//Negative Numerator
assertEquals(isProperFraction(-1, 3), true);

//Negative Denominator
assertEquals(isProperFraction(1, -3), true);

//Both Negative Numerator and Denominator
assertEquals(isProperFraction(-2, -5), true);

//Zero Numerator
assertEquals(isProperFraction(0, 5), true);

//Zero Denominator
assertEquals(isProperFraction(1, 0), false);
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,29 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
}
if (typeof card !== "string" || card.length < 2){
throw new Error("Invalid card");
}

const suits = ["♠", "♥", "♦", "♣"];
const suit = card.slice(-1);
const rank = card.slice(0,-1);

if (!suits.includes(suit)) {
throw new Error("Invalid card");
}

if (rank === "A") return 11;
if (["J", "Q", "K"].includes(rank)) return 10;

const numericValue = Number(rank);

if (numericValue >= 2 && numericValue <= 10) {
return numericValue;
}

throw new Error("Invalid card");
}

// The line below allows us to load the getCardValue function into tests in other files.
// This will be useful in the "rewrite tests with jest" step.
Expand All @@ -41,12 +62,38 @@ function assertEquals(actualOutput, targetOutput) {
// Examples:
assertEquals(getCardValue("9♠"), 9);

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

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

// Numeric Cards
assertEquals(getCardValue("2♠"), 2);
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) {}
} catch (e) {
console.log("Correctly threw error for invalid card");
}

// What other invalid card cases can you think of?

// Invliad suit
try { getCardValue("A?"); console.error("No error thrown"); } catch(e) {}

// Invalid rank
try { getCardValue("1♠"); console.error("No error thrown"); } catch(e) {}

// Missing suit
try { getCardValue("A"); console.error("No error thrown"); } catch(e) {}

// Extra characters
try { getCardValue("AAA♠"); console.error("No error thrown"); } catch(e) {}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,34 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
expect(getAngleType(89)).toEqual("Acute angle");
});

// Case 2: Right angle
// Case 2: Right angles
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 (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 is 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(270)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
})

// Case 6: Invalid angles
test('should return "Invalid angle" when angle is outside valid range', () => {
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(-10)).toEqual("Invalid angle");
expect(getAngleType(720)).toEqual("Invalid angle");
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,43 @@ 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
// Case 1: Special case: denominator is zero
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});

// Case 2: Numerator is zero
test(`should return true when numerator is zero and denominator is non-zero`, () => {
expect(isProperFraction(0, 1)).toEqual(true);
});

// Case 3: Proper fractions
test("should return true for proper fractions", () => {
expect(isProperFraction(1,2)).toEqual(true);
expect(isProperFraction(2,5)).toEqual(true);
})

// Case 4: Equal Numerator and Denominator
test("should return false when numerator equals denominator", () => {
expect(isProperFraction(2,2)).toEqual(false);
})

// Case 5: Improper fractions
test("should return false for improper fractions", () => {
expect(isProperFraction(5,3)).toEqual(false);
})

// Case 6: Negative numerator
test("should return true when numerator is negative but absolute value is smaller", () => {
expect(isProperFraction(-1, 3)).toEqual(true);
})

// Case 7: Negative denominator
test("should return true when denominator is negative but absolute value is larger", () => {
expect(isProperFraction(1, -3)).toEqual(true);
})

// Case 8: Both Negative
test("should return true when both numerator and denominator are negative and proper", () => {
expect(isProperFraction(-2, -5)).toEqual(true);
})
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,36 @@ test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
});

// Case 2: Number Cards
test("should return correct numeric value for number cards", () => {
expect(getCardValue("2♠")).toEqual(2);
expect(getCardValue("7♦")).toEqual(7);
expect(getCardValue("10♣")).toEqual(10);
})

// Case 3: Face Cards
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 throw error for invalid suit", () => {
expect(() => getCardValue("A?")).toThrow();
})

test("should throw error for invalid rank", () => {
expect(() => getCardValue("1♠")).toThrow();
});

test("should throw error for missing suit", () => {
expect(() => getCardValue("A")).toThrow();
});

test("should throw error for completely invalid string", () => {
expect(() => getCardValue("invalid")).toThrow();
});
// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
// Face Cards (J, Q, K)
Expand Down