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 @@ -16,6 +16,24 @@

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 @@ -32,6 +50,48 @@ 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");

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

// Acute angles
const acuteAngle = getAngleType(75);
assertEquals(acuteAngle, "Acute angle");

const acuteAngleBoundary = getAngleType(89);
assertEquals(acuteAngleBoundary, "Acute angle");

// Obtuse angles
const obtuseAngle = getAngleType(120);
assertEquals(obtuseAngle, "Obtuse angle");

const obtuseAngleBoundaryStart = getAngleType(91);
assertEquals(obtuseAngleBoundaryStart, "Obtuse angle");

const obtuseAngleBoundaryEnd = getAngleType(179);
assertEquals(obtuseAngleBoundaryEnd, "Obtuse angle");

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

// Reflex angles
const reflexAngle = getAngleType(270);
assertEquals(reflexAngle, "Reflex angle");

const reflexAngleBoundary = getAngleType(359);
assertEquals(reflexAngleBoundary, "Reflex angle");

// Invalid angles
const invalidAngleAboveRange = getAngleType(370);
assertEquals(invalidAngleAboveRange, "Invalid angle");

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

const invalidAngleZero = getAngleType(0);
assertEquals(invalidAngleZero, "Invalid angle");

const invalidAngleUpperBoundary = getAngleType(360);
assertEquals(invalidAngleUpperBoundary, "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (denominator === 0){
return false;
}
if(Math.abs(numerator) < Math.abs(denominator)){
return true;
}
else{
Comment on lines +15 to +21

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.

Why not use a formatter to keep the code consistently formatted?

return false;
}
}

// The line below allows us to load the isProperFraction function into tests in other files.
Expand All @@ -25,9 +34,28 @@ function assertEquals(actualOutput, targetOutput) {
`Expected ${actualOutput} to equal ${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(5, 5), false);
assertEquals(isProperFraction(2, 4), true);
assertEquals(isProperFraction(0, 9), true);
assertEquals(isProperFraction(6, 3), false);
assertEquals(isProperFraction(10,4), false);
assertEquals(isProperFraction(7, 0), false);
assertEquals(isProperFraction(15, 30), true);
assertEquals(isProperFraction(-7, 30), true);
assertEquals(isProperFraction(-9, 10), true);
assertEquals(isProperFraction(-5, -5), false);
assertEquals(isProperFraction(-1, 0), false);
assertEquals(isProperFraction(-1, -5), true);
assertEquals(isProperFraction(1, -5), true);
assertEquals(isProperFraction(0, 0), false);
assertEquals(isProperFraction(-1, 2), true);
assertEquals(isProperFraction(1, -2), true);
assertEquals(isProperFraction(-1, -2), true);
assertEquals(isProperFraction(-2, 1), false);
assertEquals(isProperFraction(2, -1), false);
assertEquals(isProperFraction(-2, -1), false);
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@

function getCardValue(card) {
// TODO: Implement this function
if(card=="A♠" || card=="A♥" || card=="A♦" || card == "A♣"){
return 11;
}
if(card=="J♠" || card=="J♥" || card=="J♦" || card == "J♣" ||
card=="Q♠" || card=="Q♥" || card=="Q♦" || card == "Q♣" ||
card=="K♠" || card=="K♥" || card=="K♦" || card == "K♣"
){
return 10;
}
Comment on lines +26 to +34

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.

Note: Code works but could probably be simplified.


const rank =card.slice(0,-1);
const suit = card.slice(-1);
const validRanks = ["2", "3", "4", "5", "6", "7", "8", "9", "10"];
const validSuits = ["♠", "♥", "♦", "♣"];
if(validRanks.includes(rank) && validSuits.includes(suit)){
return Number(rank);
}
throw new Error("Invalid card");

}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -40,7 +59,12 @@ function assertEquals(actualOutput, targetOutput) {
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:
assertEquals(getCardValue("9♠"), 9);

assertEquals(getCardValue("K♥"), 10);
assertEquals(getCardValue("J♣"), 10);
assertEquals(getCardValue("A♦"), 11);
assertEquals(getCardValue("Q♦"), 10);
assertEquals(getCardValue("10♠"), 10);
assertEquals(getCardValue("4♠"), 4);
// Handling invalid cards
try {
getCardValue("invalid");
Expand All @@ -50,5 +74,69 @@ try {
} catch (e) {
console.log("Error thrown for invalid card 🎉");
}

// What other invalid card cases can you think of?
try {
getCardValue("11♠");

// This line will not be reached if an error is thrown as expected
console.error("Error was not thrown for invalid card 😢");
} catch (e) {
console.log("Error thrown for invalid card 🎉");
}
try {
getCardValue("0");

// This line will not be reached if an error is thrown as expected
console.error("Error was not thrown for invalid card 😢");
} catch (e) {
console.log("Error thrown for invalid card 🎉");
}
try {
getCardValue("-8♦");

// This line will not be reached if an error is thrown as expected
console.error("Error was not thrown for invalid card 😢");
} catch (e) {
console.log("Error thrown for invalid card 🎉");
}
try {
getCardValue("a♣");

// This line will not be reached if an error is thrown as expected
console.error("Error was not thrown for invalid card 😢");
} catch (e) {
console.log("Error thrown for invalid card 🎉");
}
try {
getCardValue("0x02♠");
console.error("Error was not thrown for 0x02♠ 😢");
} catch (e) {
console.log("Error thrown for 0x02♠ 🎉");
}

try {
getCardValue("2.1♠");
console.error("Error was not thrown for 2.1♠ 😢");
} catch (e) {
console.log("Error thrown for 2.1♠ 🎉");
}

try {
getCardValue("0002♠");
console.error("Error was not thrown for 0002♠ 😢");
} catch (e) {
console.log("Error thrown for 0002♠ 🎉");
}

try {
getCardValue("KX");
console.error("Error was not thrown for invalid suit 😢");
} catch (e) {
console.log("Error thrown for invalid suit 🎉");
}
try {
getCardValue("5X");
console.error("Error was not thrown for invalid suit 😢");
} catch (e) {
console.log("Error thrown for invalid suit 🎉");
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,41 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
expect(getAngleType(1)).toEqual("Acute angle");
expect(getAngleType(45)).toEqual("Acute angle");
expect(getAngleType(89)).toEqual("Acute angle");
expect(getAngleType(75)).toEqual("Acute angle");
});

// Case 2: Right angle
// Case 3: Obtuse angles
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(120)).toEqual("Obtuse angle");
expect(getAngleType(91)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
expect(getAngleType(145)).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(270)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
expect(getAngleType(181)).toEqual("Reflex angle");
});
// Case 6: Invalid angles
test("should return 'Invalid angle' when angle is <= 0 or >= 360", () => {
// Test various acute angles, including boundary cases
expect(getAngleType(-10)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(370)).toEqual("Invalid angle");
expect(getAngleType(0)).toEqual("Invalid angle");
});
Comment thread
cjyuan marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,44 @@ 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`, () => {
// Denominator is zero
test("should return false when denominator is zero", () => {
expect(isProperFraction(1, 0)).toEqual(false);
expect(isProperFraction(-1, 0)).toEqual(false);
expect(isProperFraction(0, 0)).toEqual(false);
});

// Proper fractions with positive values
test("should return true when the absolute value of the numerator is less than the absolute value of the denominator", () => {

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.

Note: We could also use pseudo-code and math notations like abs(...) or | ... | to denote "absolute value of" in the descriptions.

expect(isProperFraction(1, 2)).toEqual(true);
expect(isProperFraction(2, 4)).toEqual(true);
expect(isProperFraction(15, 30)).toEqual(true);
});

// Numerator is zero
test("should return true when numerator is zero and denominator is non-zero", () => {
expect(isProperFraction(0, 9)).toEqual(true);
expect(isProperFraction(0, -9)).toEqual(true);
});

// Equal numerator and denominator
test("should return false when numerator and denominator have equal absolute values", () => {
expect(isProperFraction(5, 5)).toEqual(false);
expect(isProperFraction(-5, -5)).toEqual(false);
});

// Negative values that form proper fractions
test("should return true for negative values when the absolute numerator is less than the absolute denominator", () => {
expect(isProperFraction(-1, 2)).toEqual(true);
expect(isProperFraction(1, -2)).toEqual(true);
expect(isProperFraction(-1, -2)).toEqual(true);
});

// Positive and negative values that do not form proper fractions
test("should return false when the absolute numerator is greater than or equal to the absolute denominator", () => {
expect(isProperFraction(-2, 1)).toEqual(false);
expect(isProperFraction(2, -1)).toEqual(false);
expect(isProperFraction(-2, -1)).toEqual(false);
expect(isProperFraction(6, 3)).toEqual(false);
expect(isProperFraction(10, 4)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,30 @@ 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)
// Invalid Cards
// Case 2: 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);
});

// 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
// Case 3: Number cards (2-10)
test("should return the numeric value for number cards", () => {
expect(getCardValue("4♠")).toEqual(4);
expect(getCardValue("9♠")).toEqual(9);
expect(getCardValue("10♠")).toEqual(10);
});

// Case 4: Invalid cards
test("should throw an error for invalid cards", () => {
expect(() => getCardValue("invalid")).toThrow();
expect(() => getCardValue("11♠")).toThrow();
expect(() => getCardValue("0")).toThrow();
expect(() => getCardValue("-8♦")).toThrow();
expect(() => getCardValue("a♣")).toThrow();
expect(() => getCardValue("KX")).toThrow();
expect(() => getCardValue("5X")).toThrow();
expect(() => getCardValue("0x02♠")).toThrow();
expect(() => getCardValue("2.1♠")).toThrow();
expect(() => getCardValue("0002♠")).toThrow();
});
Loading