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

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";
}
return "Invalid angle";

}

// The line below allows us to load the getAngleType function into tests in other files.
Expand All @@ -25,13 +37,31 @@ module.exports = getAngleType;
// This helper function is written to make our assertions easier to read.
// If the actual output matches the target output, the test will pass
function assertEquals(actualOutput, targetOutput) {

console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
`❌ Error: expected ${actualOutput} to equal ${targetOutput}`
);

if (actualOutput === targetOutput) {
console.log(`✅ Test passed: ${actualOutput} equals ${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");
const acute = getAngleType(45);
assertEquals(acute, "Acute angle");
const obtuse = getAngleType(110);
assertEquals(obtuse, "Obtuse angle");
const straight = getAngleType(180);
assertEquals(straight, "Straight angle");
const reflex = getAngleType(240);
assertEquals(reflex, "Reflex angle");
const invalid = getAngleType(-10);
assertEquals(invalid, "Invalid angle");
console.log("✓ All test executed!");

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,19 @@
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function

if(denominator === 0 || numerator === 0){
return false
}

if (numerator < 0) {
numerator = numerator * -1;
}

if (denominator < 0) {
denominator = denominator * -1;
}
return numerator < denominator;
}

// The line below allows us to load the isProperFraction function into tests in other files.
Expand All @@ -30,4 +42,16 @@ function assertEquals(actualOutput, targetOutput) {
// What combinations of numerators and denominators should you test?

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(5, 9), true);
assertEquals(isProperFraction(3, 4), true);
assertEquals(isProperFraction(2, 8), true);
assertEquals(isProperFraction(1, 6), true);
assertEquals(isProperFraction(0, 5), true);
assertEquals(isProperFraction(-3, -5), true);



// Example: 3/2 is a improper fraction
assertEquals(isProperFraction(3, 0), false);
assertEquals(isProperFraction(-7, 5), false);
console.log("✓ All test executed!");
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,42 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
//check the length and if does not match the valid length throw an error
if (card.length < 2 || card.length > 3) {
throw new Error("Invalid card");
}
const suits = ["♠", "♥", "♦", "♣"];
const suit = card[card.length - 1];

let suitValid = false;

for (let i = 0; i < suits.length; i++) {
if (suit === suits[i]) {
suitValid = true;
break;
}
}
// after comparing each suit in our array and does not match throw an error
if (suitValid === false) {
throw new Error("Invalid card suit");
}

let rank = card.substring(0, card.length - 1);

if (rank === "A") {
return 11;
}
if (rank === "J" || rank === "Q" || rank === "K") {
return 10;
}

let number = Number(rank);

if (isNaN(number) || number < 2 || number > 10) {
throw new Error("Invalid card rank");
}

return number;
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -31,22 +66,52 @@ module.exports = getCardValue;

// Helper functions to make our assertions easier to read.
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
if (actualOutput !== targetOutput) {
console.error(
`❌ FAILED: Expected ${targetOutput} but received ${actualOutput}`
);
} else {
console.log(`✅ PASSED: Value is ${actualOutput} as expected`);
}
}

function assertThrows(invalidCard) {
try {
getCardValue(invalidCard);
console.error(
`❌ FAILED: "${invalidCard}" should have thrown an error, but it didn't`
);
} catch (e) {
console.log(`✅ PASSED: Error catch para "${invalidCard}" [${e.message}]`);
}
}

// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:
assertEquals(getCardValue("9♠"), 9);
console.log("--- Running Success Tests ---");
assertEquals(getCardValue("2♠"), 2);
assertEquals(getCardValue("10♥"), 10);
assertEquals(getCardValue("A♣"), 11);
assertEquals(getCardValue("K♦"), 10);
assertEquals(getCardValue("J♠"), 10);
assertEquals(getCardValue("Q♥"), 10);
assertEquals(getCardValue("7♦"), 7);

console.log("\n--- Running Edge Case Tests ---");
assertThrows("d");
assertThrows("15♥");
assertThrows("A");
assertThrows("JPP");
assertThrows("Q🌸");

// 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("✓ Error thrown as expected for invalid card");
}

// What other invalid card cases can you think of?
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 = 90)`, () => {
expect(getAngleType(90)).toEqual("Right angle");
});

// Case 3: Obtuse angles
test(`Should return "Obtuse angle" when the (angle > 90 && angle < 180)`, () => {
expect(getAngleType(160)).toEqual("Obtuse angle");
expect(getAngleType(110)).toEqual("Obtuse angle");
expect(getAngleType(140)).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 (angle > 180 && angle < 360)', () => {
expect(getAngleType(181)).toBe("Reflex angle");
expect(getAngleType(270)).toBe("Reflex angle");
expect(getAngleType(300)).toBe("Reflex angle");
});

// Case 6: Invalid angles
test(`Should return "Invalid angle" when the input is invalid`, () => {
expect(getAngleType(0)).toEqual("Invalid angle");
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 @@ -4,7 +4,26 @@ 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`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
// test: proper fraction
// --- case 1: Proper Fractions ---
test('should return true for valid proper fractions, including negative numbers', () => {
expect(isProperFraction(5, 9)).toBe(true);
expect(isProperFraction(3, 4)).toBe(true);
expect(isProperFraction(2, 8)).toBe(true);
expect(isProperFraction(1, 6)).toBe(true);
expect(isProperFraction(-3, -5)).toBe(true);
});

// --- case 2: Improper Fractions ---
test('should return false for improper fractions where numerator >= denominator', () => {
expect(isProperFraction(-7, 5)).toBe(false);
expect(isProperFraction(10, 3)).toBe(false);
expect(isProperFraction(5, 5)).toBe(false);
});

// --- case 3: 0 Numerator and 0 Denominator ---
test('should return false when numerator or denominator is zero', () => {
expect(isProperFraction(3, 0)).toBe(false);
expect(isProperFraction(0, 3)).toBe(false);
expect(isProperFraction(0, 0)).toBe(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ 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);
});

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
// Face Cards (J, Q, K)
Expand All @@ -18,3 +13,43 @@ test(`Should return 11 when given an ace card`, () => {
// please refer to the Jest documentation:
// https://jestjs.io/docs/expect#tothrowerror

describe("getCardValue", () => {

// Group 1: success cases (Valid cards)
test("should return 11 when given an Ace (A)", () => {
expect(getCardValue("A♠")).toBe(11);
expect(getCardValue("A♥")).toBe(11);
});

test("should return 10 when given a face card (J, Q, K)", () => {
expect(getCardValue("J♣")).toBe(10);
expect(getCardValue("Q♦")).toBe(10);
expect(getCardValue("K♠")).toBe(10);
});

test("should return the numeric value for number cards (2-10)", () => {
expect(getCardValue("2♥")).toBe(2);
expect(getCardValue("7♦")).toBe(7);
expect(getCardValue("10♣")).toBe(10);
});

// Group 2: error cases (Invalid cards)
describe("Invalid cards", () => {
test("should throw an error for invalid card length or missing components", () => {
expect(() => getCardValue("A")).toThrow("Invalid card");
expect(() => getCardValue("100♥")).toThrow("Invalid card");
expect(() => getCardValue("")).toThrow("Invalid card");
});

test("should throw an error for an invalid suit emoji", () => {
expect(() => getCardValue("A🌸")).toThrow("Invalid card suit");
expect(() => getCardValue("10X")).toThrow("Invalid card suit");
});

test("should throw an error for an invalid rank", () => {
expect(() => getCardValue("1♥")).toThrow("Invalid card rank");
expect(() => getCardValue("15♦")).toThrow("Invalid card rank");
expect(() => getCardValue("Z♠")).toThrow("Invalid card rank");
});
});
});