Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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 >= 360) {
return "Invalid angle";
} else if (angle > 0 && angle < 90) {
return "Acute angle";
} else if (angle > 90 && angle < 180) {
return "Obtuse angle";
} else if (angle === 90) {
return "Right angle";
} else if (angle===180) {
return "Straight angle";
} else if (angle > 180 && angle < 360) {
return "Reflex angle";
}
}

// The line below allows us to load the getAngleType function into tests in other files.
Expand All @@ -34,4 +46,22 @@ function assertEquals(actualOutput, targetOutput) {
// TODO: Write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles
const right = getAngleType(90);
const acute1 = getAngleType(20);
const invalid = getAngleType(0);
const acute3 = getAngleType(89.999);
const obtuse = getAngleType(98.25);
const straightAngle = getAngleType(180);
const invalid1 = getAngleType(360);
const invalidAngle = getAngleType(362);
const reflexAngle2 = getAngleType(210);

assertEquals(right, "Right angle");
assertEquals(acute1, "Acute angle");
assertEquals(invalid, "Invalid angle");
assertEquals(obtuse, "Obtuse angle");
assertEquals(straightAngle, "Straight angle");
assertEquals(invalid1, "Invalid angle");
assertEquals(invalidAngle, "Invalid angle");
assertEquals(reflexAngle2, "Reflex angle");
assertEquals(acute3, "Acute angle");

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

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if ( Math.abs(numerator) >= Math.abs(denominator)) {
return false;
} else if (Math.abs(denominator) === 0) {
return false;
} else {
return true;
}
}

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

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(1, 10000), true);
assertEquals(isProperFraction(100, 2), false);
assertEquals(isProperFraction(1e2, 1e3), true);
assertEquals(isProperFraction(1e4, 1), false);
assertEquals(isProperFraction(-1, 1), false);
assertEquals(isProperFraction(1, -1), false);
assertEquals(isProperFraction(0, -1), true);
assertEquals(isProperFraction(-1, -2), true);
assertEquals(isProperFraction(1, -2), true);
assertEquals(isProperFraction(-1, 2), true);
assertEquals(isProperFraction(1, 0), false);
assertEquals(isProperFraction(-1, 0), false);




Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"
// The suit can be one of the following emojis:
// "♠", "♥", "♦", "♣"
// For example: "A♠", "2♥", "10♥", "J", "Q♦", "K♦".
// For example: "A♠", "2♥", "10♥", "J"2♠"", "Q♦", "K♦".

// When the card is an ace ("A"), the function should return 11.
// When the card is a face card ("J", "Q", "K"), the function should return 10.
Expand All @@ -22,7 +22,36 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
if (card.length >= 4) {
throw new Error ("Invalid String");
}
if (
!card.includes("♠") &&
!card.includes("♥") &&
!card.includes("♦") &&
!card.includes("♣")
) {
throw new Error('Invalid String');
}
const rank = card.slice(0, -1);

if (
!card.includes("A") &&
!card.includes("J") &&
!card.includes("Q") &&
!card.includes("K") &&
!(rank >= 2 && rank <= 10)
) {
throw new Error("Invalid String");
}

if (rank === "A") {
return 11;
} else if (rank === "J" || rank === "Q" || rank === "K") {
return 10;
} else if (rank >= 2 && rank <= 10) {
return Number(rank);
}
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -40,13 +69,40 @@ 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("K♠"), 10);
assertEquals(getCardValue("Q♠"), 10);
assertEquals(getCardValue("2♠"), 2);
assertEquals(getCardValue("10♠"), 10);



// Handling invalid cards
try {
getCardValue("invalid");
function throwInvalidStringError(card){

try {
getCardValue(card)
// 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) {
assertEquals(e.message,"Invalid String")
}
}

throwInvalidStringError("invalid");
throwInvalidStringError(" ♠");
throwInvalidStringError(" 6 ♠");
throwInvalidStringError(" ♠");
throwInvalidStringError(" **♠");
throwInvalidStringError("10 **");
throwInvalidStringError("A10♠");
throwInvalidStringError("0x02♠");
throwInvalidStringError("2.1♠");
throwInvalidStringError("0002♠");
throwInvalidStringError("22");
throwInvalidStringError("♠2");

// What other invalid card cases can you think of?
// card with a mix of suit
//card with 0 at the front 01♠
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,40 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

// Case 2: Right angle
test(`should return "Right Angles" when(angle===90)`, () => {
expect(getAngleType(90)).toEqual("Right angle");
});

// Case 3: Obtuse angles

test(`should return "Obtuse angles" when (90< angle <180)`, () => {
expect(getAngleType(91)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
expect(getAngleType(160)).toEqual("Obtuse angle");
expect(getAngleType(100)).toEqual("Obtuse angle");
expect(getAngleType(151.55)).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 (360<angle>180)`, () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

360<angle>180 -- This notation seems to suggest "angle is greater than 360 and is greater than 180". Can you revise it?

expect(getAngleType(181)).toEqual("Reflex angle");
expect(getAngleType(270)).toEqual("Reflex angle");
expect(getAngleType(350)).toEqual("Reflex angle");
expect(getAngleType(359.5)).toEqual("Reflex angle");
expect(getAngleType(210)).toEqual("Reflex angle");
expect(getAngleType(200)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test(`Should return "Invalid angle" when (0 =<angle>=360)`, () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

How about (angle <= 0 or angle >=360)?

expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(-1)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(500)).toEqual("Invalid angle");
expect(getAngleType("1e5")).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ 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(1, -1)).toEqual(false);
expect(isProperFraction(10, 1)).toEqual(false);
expect(isProperFraction(1, 10)).toEqual(true);
expect(isProperFraction(-1, 0)).toEqual(false);
expect(isProperFraction(1e2, 1e4)).toEqual(true);
expect(isProperFraction(1e4, 1e0)).toEqual(false);
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The test description does not quite describe the tests being carried out.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These data categories and samples are not comprehensive enough. You should test also those proper and improper fraction samples you had in implement/2-is-proper-fraction.js.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Alright, I will improve the test case. Thank you very much

Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,44 @@ test(`Should return 11 when given an ace card`, () => {

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
test(`should return Number from "2-10" 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);
})


// Face Cards (J, Q, K)

test(`Should return 10 when given a face card`,()=>{

expect(getCardValue("J♣")).toEqual(10);
expect(getCardValue("K♣")).toEqual(10);
expect(getCardValue("Q♣")).toEqual(10);
expect(getCardValue("Q♠")).toEqual(10);
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("K♠")).toEqual(10);
})
// Invalid Cards

test('throws on Invalid String', () => {
expect(() => { getCardValue('A10♠');}).toThrow("Invalid String");
expect(() => { getCardValue('A');}).toThrow("Invalid String");
expect(() => { getCardValue('110♠');}).toThrow("Invalid String");
expect(() => { getCardValue(' ♠');}).toThrow("Invalid String");
expect(() => { getCardValue('eeeeeej1234');}).toThrow("Invalid String");

});



// 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
Expand Down
Loading