Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,24 @@ 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 invalidLow = getAngleType(-10);
assertEquals(invalidLow, "Invalid angle");

const invalidHigh = getAngleType(400);
assertEquals(invalidHigh, "Invalid angle");



Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@

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

// The line below allows us to load the isProperFraction function into tests in other files.
Expand All @@ -31,3 +44,15 @@ function assertEquals(actualOutput, targetOutput) {

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);

assertEquals(isProperFraction(2, 1), false);

assertEquals(isProperFraction(0, 1), true);

assertEquals(isProperFraction(1, 0), false);

assertEquals(isProperFraction(-1, 2), true);

assertEquals(isProperFraction(1, -2), false);

assertEquals(isProperFraction(-1, -2), true);
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@

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

// Checking if the card is valid
const rank = card.slice(0, -1);
const suit = card.slice(-1);

if (!validRanks.includes(rank) || !validSuits.includes(suit)) {
throw new Error("Invalid card");
}

// Determininig the value of the card
if (rank === "A") {
return 11;
} else if (["J", "Q", "K"].includes(rank)) {
return 10;
} else {
return parseInt(rank, 10);
}
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -40,6 +59,11 @@ 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);


// Handling invalid cards
try {
Expand All @@ -52,3 +76,5 @@ try {
}

// What other invalid card cases can you think of?
assertEquals(getCardValue("11♥"), 11);
assertEquals(getCardValue("A♦"), 11);
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,31 @@ 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)`, () => {
// Test various obtuse angles, including boundary cases
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)`, () => {
// Test various reflex angles, including boundary cases
expect(getAngleType(181)).toEqual("Reflex angle");
expect(getAngleType(270)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});
// Case 6: Invalid angles
test(`should throw an error when angle is negative`, () => {
expect(() => getAngleType(-1)).toThrow("Invalid angle");
});
test(`should throw an error when angle is greater than or equal to 360`, () => {
expect(() => getAngleType(360)).toThrow("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,63 @@
// 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.
const isProperFraction = require("../implement/2-is-proper-fraction");

// 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);
});

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

// Case 3: Zero numerator
test(`should return true when numerator is zero`, () => {
expect(isProperFraction(0, 1)).toEqual(true);
expect(isProperFraction(0, -1)).toEqual(true);
});

// Case 4: Zero denominator
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);
});

// Case 5: Negative fractions
test(`should return false for negative fractions with positive denominator`, () => {
expect(isProperFraction(-1, 2)).toEqual(true);
});

test(`should return false for negative fractions with negative denominator`, () => {
expect(isProperFraction(-1, -2)).toEqual(true);
});

// Case 6: 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);
});

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


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

Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,44 @@ const getCardValue = require("../implement/3-get-card-value");

// TODO: Write tests in Jest syntax to cover all possible outcomes.


// Case 1: Ace (A)
test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
expect(getCardValue("A♥")).toEqual(11);
expect(getCardValue("A♦")).toEqual(11);
expect(getCardValue("A♣")).toEqual(11);
});

// Case 2: Face cards (J, Q, K)
test(`Should return 10 when given a face card`, () => {
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("Q♥")).toEqual(10);
expect(getCardValue("K♦")).toEqual(10);
});

// Case 3: Number cards (2-10)
test(`Should return the numeric value when given a number card`, () => {
expect(getCardValue("2♠")).toEqual(2);
expect(getCardValue("3♥")).toEqual(3);
expect(getCardValue("4♦")).toEqual(4);
expect(getCardValue("5♣")).toEqual(5);
expect(getCardValue("6♠")).toEqual(6);
expect(getCardValue("7♥")).toEqual(7);
expect(getCardValue("8♦")).toEqual(8);
expect(getCardValue("9♣")).toEqual(9);
expect(getCardValue("10♠")).toEqual(10);
});

// Case 4: Invalid cards
test(`Should throw an error when given an invalid card`, () => {
expect(() => getCardValue("invalid")).toThrow("Invalid card");
expect(() => getCardValue("1♠")).toThrow("Invalid card");
expect(() => getCardValue("11♥")).toThrow("Invalid card");
expect(() => getCardValue("A♦")).not.toThrow(); // Valid ace card
expect(() => getCardValue("J♣")).not.toThrow(); // Valid face card
});

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
// Face Cards (J, Q, K)
Expand Down
Loading