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

// The line below allows us to load the getAngleType function into tests in other files.
Expand All @@ -31,7 +43,10 @@ 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");
assertEquals(getAngleType(90), "Right angle");
assertEquals(getAngleType(45), "Acute angle");
assertEquals(getAngleType(91), "Obtuse angle");
assertEquals(getAngleType(180), "Straight angle");
assertEquals(getAngleType(185), "Reflex angle");
assertEquals(getAngleType(365), "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (isNaN(numerator) && isNaN(denominator)) {
return false;
}

if (numerator < denominator) {
return true;
}
return false;
}

// The line below allows us to load the isProperFraction function into tests in other files.
Expand All @@ -26,8 +33,11 @@ function assertEquals(actualOutput, targetOutput) {
);
}

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

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(2, 2), false);
assertEquals(isProperFraction(3, 2), false);
assertEquals(isProperFraction(4312, 5000), true);
assertEquals(isProperFraction("Invalid", "data"), false);
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,48 @@
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.

function isSuit(suit) {
if (suit === "♠" || suit === "♥" || suit === "♦" || suit === "♣") {
return true;
}
return false;
}

function getCardValue(card) {
// TODO: Implement this function
// the length has to be 2 or 3 then we pass this check

if (typeof card !== "string") {
throw new Error(
`Expected type for ${card} is string, but got ${typeof card}`
);
}

if (card.length !== 2 && card.length !== 3) {
throw new Error(`Invalid input length for ${card}`);
}

const lastCharIndex = card.length - 1;
const rank = card.slice(0, lastCharIndex);
const suit = card.slice(lastCharIndex);

if (!isSuit(suit)) {
throw new Error(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think this works like you think it does. Can you update your tests to show that the error message is what you expect it to be?

`Invalid last character detected, only suits(♠♥♦♣) are allowed, but got this character: ${suit}`
);
}

if (rank === "A") {
return 11;
} else if (rank === "J" || rank === "Q" || rank === "K") {
return 10;
} else if (!isNaN(rank) && Number(rank) >= 2 && Number(rank) <= 10) {
// check if number and within bounds
return Number(rank);
} else {
throw new Error(
`Invalid rank "${rank}" in card "${card}": expected A, J, Q, K, or a number between 2 and 10`
);
}
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,36 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

// Case 2: Right angle
test(`should return "Right angle" when (angle == 90)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(90)).toEqual("Right angle");
});

// Case 3: Obtuse angles
test(`should return "Obtuse" when (90 < angle < 180)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(91)).toEqual("Obtuse angle");
expect(getAngleType(140)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
});

// Case 4: Straight angle
test(`should return "Straight angle" when (angle == 180)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(180)).toEqual("Straight angle");
});

// Case 5: Reflex angles
test(`should return "Reflex angle" when (180 < angle < 360)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(181)).toEqual("Reflex angle");
expect(getAngleType(220)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test(`should return "Invalid angle"`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(361)).toEqual("Invalid angle");
expect(getAngleType("Test data")).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
// Special case: numerator is zero
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
expect(isProperFraction(2, 2)).toEqual(false);
expect(isProperFraction(3, 2)).toEqual(false);
expect(isProperFraction(3, 4)).toEqual(true);
expect(isProperFraction(4312, 5000)).toEqual(true);
expect(isProperFraction("invalid", "data")).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,36 @@ 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("AA♠")).toThrow(Error);
});

// Case 2
test(`Should return 10 when given a jack, queen, king`, () => {
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("Q♥")).toEqual(10);
expect(getCardValue("K♠")).toEqual(10);
expect(() => getCardValue("K🤣")).toThrow(
"Invalid last character detected, only suits(♠♥♦♣) are allowed, but got this character:"
);
expect(() => getCardValue("KKW🤣")).toThrow(Error);
});

// Case 3
test(`Should return number when given a number`, () => {
expect(getCardValue("2♠")).toEqual(2);
expect(getCardValue("10♥")).toEqual(10);
expect(() => getCardValue("0♠")).toThrow(Error);
expect(() => getCardValue("1♠")).toThrow(Error);
expect(() => getCardValue("10Q")).toThrow(Error);
expect(() => getCardValue("423🤣")).toThrow(Error);
});

test("Should return error on invalid input", () => {
expect(() => getCardValue(null)).toThrow(Error);
expect(() => getCardValue(undefined)).toThrow(Error);
expect(() => getCardValue(12398)).toThrow(Error);
expect(() => getCardValue(NaN)).toThrow(Error);
expect(() => getCardValue("♠")).toThrow(Error);
});

// Suggestion: Group the remaining test data into these categories:
Expand All @@ -17,4 +47,3 @@ test(`Should return 11 when given an ace card`, () => {
// 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