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 @@ -14,10 +14,24 @@
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.

function getAngleType(angle) {
// TODO: Implement this function
function getAngleType(angle) {
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.
module.exports = getAngleType;
Expand All @@ -33,5 +47,55 @@ function assertEquals(actualOutput, targetOutput) {

// TODO: Write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles

// Acute angle test
const acute1 = getAngleType(45);
assertEquals(acute1, "Acute angle");

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

const acute3 = getAngleType(89.999);
assertEquals(acute3, "Acute angle");

// Right angle test
const right = getAngleType(90);
assertEquals(right, "Right angle");

// Obtuse angle test
const obtuse1 = getAngleType(120);
assertEquals(obtuse1, "Obtuse angle");

const obtuse2 = getAngleType(179.999);
assertEquals(obtuse2, "Obtuse angle");

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

// Reflex angle test
const reflex1 = getAngleType(200);
assertEquals(reflex1, "Reflex angle");

const reflex2 = getAngleType(359.999);
assertEquals(reflex2, "Reflex angle");

// Invalid angle test
const invalid1 = getAngleType(0);
assertEquals(invalid1, "Invalid angle");

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

const invalid3 = getAngleType(-10);
assertEquals(invalid3, "Invalid angle");

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


console.log("All tests executed.");

// Note: If no assertion errors are thrown, all tests have passed succesfully.
// i feel there is a better way or cleaner way to write the test, but for now,
// this is a simple way to cover all cases.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,27 @@
// Assumption: The parameters are valid numbers (not NaN or Infinity).

// Note: If you are unfamiliar with proper fractions, please look up its mathematical definition.
// A proper fraction means: The top number (numerator) is smaller than the bottom number (denominator).
// e.g. 1/2, 3/5. 5/2 would not be a proper fraction.
// Numerator < Denominator

// Acceptance criteria:
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
function isProperFraction(numerator, denominator) {
// A fraction cannot have denominator 0
if (denominator === 0) {
return false;
}

// A proper fraction means numerator is smaller than denominator
if (numerator < 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 +42,28 @@ function assertEquals(actualOutput, targetOutput) {

// TODO: Write tests to cover all cases.
// What combinations of numerators and denominators should you test?
// I should test proper fractions, equal numbers, improper fractions, zero numerator,
// zero denominator and negative numbers.

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

// Equal numbers (not proper)
assertEquals(isProperFraction(4, 4), false);

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

// Zero numerator (0 is less than denominator)
assertEquals(isProperFraction(0, 5), true);

// Zero denominator (invalid fraction)
assertEquals(isProperFraction(5, 0), false);

// Negative numbers
assertEquals(isProperFraction(-1, 5), true);
assertEquals(isProperFraction(1, -5), false);

console.log("All tests executed.");
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,47 @@
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.

// TODO: Implement this function
function getCardValue(card) {
// TODO: Implement this function
// Valid suits
const validSuits = ["♠", "♥", "♦", "♣"];

// Card must be at least 2 characters (e.g., "A♠")
if (typeof card !== "string" || card.length < 2) {
throw new Error("Invalid card");
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good error check here!


// Suit is always the last character

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your comments are also good, but for one line code, it might not be necessary, but still it's good practice👍

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. I did extra notes so I can refer back to it and understand what I did.

const suit = card.slice(-1);

// Rank is everything except the last character
const rank = card.slice(0, -1);

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

// Handle Ace
if (rank === "A") {
return 11;
}

// Handle face cards
if (rank === "J" || rank === "Q" || rank === "K") {
return 10;
}

// Handle number cards (2–10)
const number = Number(rank);

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

// If nothing matched → invalid
throw new Error("Invalid card");

}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -38,15 +77,47 @@ function assertEquals(actualOutput, targetOutput) {
}

// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Test: Ace, face cards, number cards, invalid suit, invalid rank, completely invalid string.
// Examples:
// Number cards:
assertEquals(getCardValue("9♠"), 9);
assertEquals(getCardValue("2♠"), 2);
assertEquals(getCardValue("10♦"), 10);

// Face card:
assertEquals(getCardValue("J♣"), 10);
assertEquals(getCardValue("Q♦"), 10);
assertEquals(getCardValue("K♠"), 10);

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

// Handling invalid cards
try {
getCardValue("invalid");
console.error("Error was not thrown for invalid string");
} catch (e) {}

// Invalid rank
try {
getCardValue("1♠");
console.error("Error wasnot thrown for invalid rank");
} catch (e) {}

// This line will not be reached if an error is thrown as expected
console.error("Error was not thrown for invalid card");
// Invalid suit
try {
getCardValue("A?");
console.error("Error wasnot thrown for invalid suit");
} catch (e) {}

// Missing suit
try {
getCardValue("A");
console.error("Error wasnot thrown for missing suit");
} catch (e) {}

console.log("All tests executed.");


// What other invalid card cases can you think of?
// Test: Ace, face cards, number cards, invalid suit, invalid rank, completely invalid string.
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 is 90`, () => {
expect(getAngleType(90)).toEqual("Right angle");
});

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

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

// Zero numerator
test(`should return true when numerator is 0 and denominator positive`, () => {
expect(isProperFraction(0, 5)).toEqual(true);
});

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

// Improper fractions (positive)
test(`should return false for improper positive fractions`, () => {
expect(isProperFraction(5, 2)).toEqual(false);
expect(isProperFraction(10, 3)).toEqual(false);
expect(isProperFraction(4, 4)).toEqual(false); // numerator = denominator
});

// Negative fractions
test(`should handle negative numerators`, () => {
expect(isProperFraction(-1, 5)).toEqual(true);
});

test(`should handle negative denominators`, () => {
expect(isProperFraction(1, -5)).toEqual(false);
});

test(`should handle both numerator and denominator negative`, () => {
expect(isProperFraction(-2, -5)).toEqual(true);
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,34 @@ test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
});

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

// Case 3: Face cards (J, Q, K)
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 cards`, () => {
expect(() => getCardValue("1♠")).toThrow("Invalid card"); // invalid rank
expect(() => getCardValue("A?")).toThrow("Invalid card"); // invalid suit
expect(() => getCardValue("10♠♠")).toThrow("Invalid card"); // extra character
expect(() => getCardValue("")).toThrow("Invalid card"); // empty string
expect(() => getCardValue("banana")).toThrow("Invalid card"); // completely invalid
});

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

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

// https://jestjs.io/docs/expect#tothrowerror
Loading