Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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,8 +14,23 @@
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.

//Question:
// What about 360 angle that is a valid angle and called complete angle should I include that as well?
function getAngleType(angle) {
// TODO: Implement this function
if (angle <= 0 || angle >= 360) {
return "Invalid angle";
} else if (angle < 90) {
return "Acute angle";
} else if (angle === 90) {
return "Right angle";
} else if (angle < 180) {
return "Obtuse angle";
} else if (angle === 180) {
return "Straight angle";
} else if (angle < 360) {
return "Reflex angle";
}
}

// The line below allows us to load the getAngleType function into tests in other files.
Expand All @@ -33,5 +48,27 @@ 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");
let angle = getAngleType(0);
assertEquals(angle, "Invalid angle");
angle = getAngleType(-78);
assertEquals(angle, "Invalid angle");
angle = getAngleType(1);
assertEquals(angle, "Acute angle");
angle = getAngleType(90);
assertEquals(angle, "Right angle");
angle = getAngleType(91);
assertEquals(angle, "Obtuse angle");
angle = getAngleType(180);
assertEquals(angle, "Straight angle");
angle = getAngleType(181);
assertEquals(angle, "Reflex angle");
angle = getAngleType(360);
assertEquals(angle, "Invalid angle");
angle = getAngleType(45671);
assertEquals(angle, "Invalid angle");
angle = getAngleType(0.5);
assertEquals(angle, "Acute angle");
angle = getAngleType(90.0098);
assertEquals(angle, "Obtuse angle");
angle = getAngleType(-0.0001);
assertEquals(angle, "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
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 @@ -31,3 +33,12 @@ function assertEquals(actualOutput, targetOutput) {

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(2, 2), false);
assertEquals(isProperFraction(3, 2), false);
assertEquals(isProperFraction(0, 2), true);
assertEquals(isProperFraction(-1, 2), true);
assertEquals(isProperFraction(-1, -2), true);
assertEquals(isProperFraction(1, -2), true);
assertEquals(isProperFraction(0, -1), true);
assertEquals(isProperFraction(100, -100), false);
assertEquals(isProperFraction(-100, 100), false);
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,32 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
// Handling invalid cards
try{
const rank = card.slice(0,-1); //rank of the card is everything except the last character of card string
const suit = card.slice(-1); // suit is the last character of the card string
if((card.length != 2 && card.length !=3) || !isValidCard(rank, suit)){
throw new Error("Invalid card");
}
Comment thread
cjyuan marked this conversation as resolved.
Outdated
if(rank === "J" || rank === "Q" || rank == "K")
return 10
else if(rank == "A")
return 11;
else
return Number(rank);
}
catch(e){
return e.message;
}
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.

This requirement has not yet been met:

// When the card string is invalid (not following the above format), the function should
// throw an error

Currently your function returns a string when a card is invalid.

Note: The error is supposed to be thrown to the caller.

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.

Yes that's right I was not throwing an error. I have fixed it now.

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.

I have created a separate function with the name assertThrows in the 3-get-card-value.js file to catch the error. But, I am not sure how can I test this thrown error in 3-get-card-value.test.js file where I am using jest test.

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.

See lines 43-45 in 3-get-card-value.test.js

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

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.

Thanks. I should have looked at this. Fixed now.

}

function isValidCard(rank,suit){
const ranks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
const suits = ["♠", "♥", "♦", "♣"];
if(ranks.includes(rank) && suits.includes(suit))
return true;
else
return false;
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -41,12 +66,14 @@ function assertEquals(actualOutput, targetOutput) {
// Examples:
assertEquals(getCardValue("9♠"), 9);

// 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) {}

// What other invalid card cases can you think of?
assertEquals(getCardValue("10♠"), 10);
assertEquals(getCardValue("Q♠"), 10);
assertEquals(getCardValue("A♣"), 11);
assertEquals(getCardValue("2♠"), 2);
assertEquals(getCardValue("J♦"), 10);
assertEquals(getCardValue("K♠"), 10);
assertEquals(getCardValue("Invalid"), "Invalid card");
assertEquals(getCardValue("1Q"), "Invalid card");
assertEquals(getCardValue("-10♦"), "Invalid card");
assertEquals(getCardValue("♦K"), "Invalid card");
assertEquals(getCardValue("Q♦♦"), "Invalid card");
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 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 angle" when (90 < angle < 180)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(91)).toEqual("Obtuse angle");
expect(getAngleType(112)).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(223)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test(`should return "Invalid angle" when angle >= 360 or angle <= 0)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(-1)).toEqual("Invalid angle");
expect(getAngleType(-2292)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(365)).toEqual("Invalid angle");
expect(getAngleType(383949)).toEqual("Invalid angle");
});
Comment thread
cjyuan marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,15 @@ 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(0, 1)).toEqual(true);
expect(isProperFraction(0, -1)).toEqual(true);
expect(isProperFraction(1, 1)).toEqual(false);
expect(isProperFraction(1, 2)).toEqual(true);
expect(isProperFraction(1, -2)).toEqual(true);
expect(isProperFraction(-6, -2)).toEqual(false);
expect(isProperFraction(-2, -6)).toEqual(true);
expect(isProperFraction(-200, -0)).toEqual(false);
expect(isProperFraction(-0, -1000)).toEqual(true);
expect(isProperFraction(5, 3)).toEqual(false);
expect(isProperFraction(-25, 3)).toEqual(false);
Comment thread
cjyuan marked this conversation as resolved.
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,32 @@ test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
});

// Case 2: Number Cards (2-10)
test(`Should return number when given a number`, () => {
Comment thread
cjyuan marked this conversation as resolved.
Outdated
expect(getCardValue("2♠")).toEqual(2);
expect(getCardValue("10♠")).toEqual(10);
expect(getCardValue("5♠")).toEqual(5);
});

// Case 3: Face Cards (J, Q, K)
test(`Should return 10 when given a face card`, () => {
expect(getCardValue("Q♠")).toEqual(10);
expect(getCardValue("K♠")).toEqual(10);
expect(getCardValue("J♠")).toEqual(10);
});

// Case 4: Invalid Cards
test(`Should return Invalid Card when given an invalid card`, () => {
expect(getCardValue("Aas♠")).toEqual("Invalid card");
expect(getCardValue("What")).toEqual("Invalid card");
expect(getCardValue("Q10")).toEqual("Invalid card");
expect(getCardValue("11")).toEqual("Invalid card");
expect(getCardValue("♠11")).toEqual("Invalid card");
expect(getCardValue("10*")).toEqual("Invalid card");
expect(getCardValue("Q_")).toEqual("Invalid card");
expect(getCardValue("A10")).toEqual("Invalid card");
});

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
// Face Cards (J, Q, K)
Expand All @@ -17,4 +43,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