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,9 +15,13 @@
// 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 "Acute angle";
if (angle === 90) return "Right angle";
if (angle < 180) return "Obtuse angle";
if (angle === 180) return "Straight angle";
if (angle < 360) return "Reflex 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 All @@ -35,3 +39,47 @@ function assertEquals(actualOutput, targetOutput) {
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");

const straight = getAngleType(180);
assertEquals(straight, "Straight angle");

const acute_upper_boundary = getAngleType(89);
assertEquals(acute_upper_boundary, "Acute angle");

const acute_lower_boundary = getAngleType(1);
assertEquals(acute_lower_boundary, "Acute angle");

const invalid_negative = getAngleType(-1);
assertEquals(invalid_negative, "Invalid angle");

const invalid_zero = getAngleType(0);
assertEquals(invalid_zero, "Invalid angle");

const invalid_full = getAngleType(360);
assertEquals(invalid_full, "Invalid angle");

const invalid_big = getAngleType(361);
assertEquals(invalid_big, "Invalid angle");

const obtuse_lower_boundary = getAngleType(91);
assertEquals(obtuse_lower_boundary, "Obtuse angle");

const obtuse_upper_boundary = getAngleType(179);
assertEquals(obtuse_upper_boundary, "Obtuse angle");

const reflex_lower_boundary = getAngleType(181);
assertEquals(reflex_lower_boundary, "Reflex angle");

const reflex_upper_boundary = getAngleType(359);
assertEquals(reflex_upper_boundary, "Reflex angle");

//decimal test cases

const acute_decimal = getAngleType(89.9);
assertEquals(acute_decimal, "Acute angle");

const obtuse_decimal = getAngleType(179.9);
assertEquals(obtuse_decimal, "Obtuse angle");

const reflex_decimal = getAngleType(180.1);
assertEquals(reflex_decimal, "Reflex angle");
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (denominator === 0 || numerator === 0) return false;
if (!(denominator % 1 === 0 && numerator % 1 === 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 @@ -28,6 +31,25 @@ function assertEquals(actualOutput, targetOutput) {

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

// definition is |numerator| < |denominator| so negative values need to be tested as well; also 0 for either - wouldn't be a fraction (or a valid fraction if its the den); also when the num = den
// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(-3, 4), true);
assertEquals(isProperFraction(3, -4), true);

//numerator or denominator is 0
assertEquals(isProperFraction(0, 2), false);
assertEquals(isProperFraction(2, 0), false);

//result is a whole number
assertEquals(isProperFraction(-2, 2), false);
assertEquals(isProperFraction(-3, -3), false);
assertEquals(isProperFraction(3, 3), false);

//numerator or denominator is a decimal
assertEquals(isProperFraction(1.2, 3), false);
assertEquals(isProperFraction(1, 1.2), false);

//numerator or denominator is negative
assertEquals(isProperFraction(-4, 5), true);
assertEquals(isProperFraction(-5, 4), false);
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,26 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
const suits = ["♠", "♥", "♦", "♣"];
if (card[0] === "A" && suits.includes(card[1]) && card.length === 2)
return 11;
if (
(card[0] === "J" || card[0] === "Q" || card[0] === "K") &&
suits.includes(card[1]) &&
card.length === 2
)
return 10;
if (
["2", "3", "4", "5", "6", "7", "8", "9"].includes(card[0]) &&
suits.includes(card[1]) &&
card.length === 2
)
return Number(card[0]);
if (card.slice(0, 2) == "10" && suits.includes(card[2]) && card.length === 3)
return 10;
throw new Error(
'Invalid input: Expected rank followed by a suit symbol. For example: "A♠", "2♥", "10♥", "J♣", "Q♦", "K♦"'
);
}

// 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("Q♦"), 10);
assertEquals(getCardValue("K♣"), 10);
assertEquals(getCardValue("10♥"), 10);
assertEquals(getCardValue("2♦"), 2);
assertEquals(getCardValue("7♦"), 7);

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

// What other invalid card cases can you think of?
// for instance "1♦" where it starts with 1 but its not 10
// where it includes the correct answer but there are added characters maybe space(s) like "9♠ "
//where its the opposite way : "♠9"
//where it starts with a valid number but has otherwise more digits eg: "9♠♠" or "99♠"
//where there is a different symbol from the 4 given such as ❤️

try {
getCardValue("1♥");

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

try {
getCardValue("9♠ ");

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

try {
getCardValue("♠9");

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

try {
getCardValue("99♠");

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

try {
getCardValue("9♠♠");

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

try {
getCardValue("9❤️");

// 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 🎉");
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,37 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

// Case 2: Right angle
test('should return "Right angle" when (angle = 90)', () => {
// Test right angle
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(100)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
});

// Case 4: Straight angle
test('should return "Straight angle" when (angle = 180)', () => {
// Test straight angle
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(300)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test('Should return "Invalid angle" when the input is invalid', () => {
expect(getAngleType(-1)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(361)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,37 @@
// 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.
// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.]

// Special case: numerator is zero
//case 1: proper function

test("should return true when |numerator| < |denominator|", () => {
expect(isProperFraction(-3, 4)).toEqual(true);
expect(isProperFraction(3, -4)).toEqual(true);
expect(isProperFraction(1, 2)).toEqual(true);
});

//case 2: result is a whole number
test("should return false when the result is a whole number", () => {
expect(isProperFraction(2, 2)).toEqual(false);
expect(isProperFraction(-2 - 2)).toEqual(false);
expect(isProperFraction(2, -2)).toEqual(false);
});

//case 3: numerator or denominator is a decimal
test("should return false if the numerator or denominator is a decimal", () => {
expect(isProperFraction(1.5, 2)).toEqual(false);
expect(isProperFraction(1, 1.5)).toEqual(false);
});

//case 4: numerator or denominator is negative
test("should return true even if the numerator or denominator is negative", () => {
expect(isProperFraction(-4, 5)).toEqual(true);
expect(isProperFraction(4, -5)).toEqual(true);
});

// Special case: numerator or denominator is zero
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
expect(isProperFraction(0, 1)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,43 @@ const getCardValue = require("../implement/3-get-card-value");
// 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);
});

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
// Face Cards (J, Q, K)
// Case 2: Number Cards (2-10)
test(`Should return the number 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 3: 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);
});

// Invalid Cards
test(`Should return an error when given a invalid input`, () => {
expect(() => getCardValue("J♠ ")).toThrow();
expect(() => getCardValue("Q")).toThrow();
expect(() => getCardValue("9♠ ")).toThrow();
expect(() => getCardValue("♠9")).toThrow();
expect(() => getCardValue("♠99")).toThrow();
expect(() => getCardValue("99♠")).toThrow();
expect(() => getCardValue("9❤️")).toThrow();
});

// 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