Skip to content
Open
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Implement a function getAngleType
//
// When given an angle in degrees, it should return a string indicating the type of angle:
// - "Acute angle" for angles greater than 0° and less than 90°
// - "Right angle" for exactly 90°
Expand All @@ -15,12 +14,32 @@
// execute the code to ensure all tests pass.

function getAngleType(angle) {
// TODO: Implement this function
if (angle <= 0 || angle >= 360) {
return "Invalid angle";
}

if (angle < 90) {
return "Acute angle";
}
if (angle === 90) {
return "Right angle";
}
if (angle < 180) {
return "Obtuse angle";
}
if (angle === 180) {
return "Straight angle";
}
if (angle < 360) {
return "Reflex angle";
}
}
module.exports = getAngleType;

// TODO: Implement this function

// The line below allows us to load the getAngleType function into tests in other files.
// This will be useful in the "rewrite tests with jest" step.
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
Expand All @@ -35,3 +54,11 @@ function assertEquals(actualOutput, targetOutput) {
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");

// Test
assertEquals(getAngleType(45), "Acute angle");
assertEquals(getAngleType(90), "Right angle");
assertEquals(getAngleType(120), "Obtuse angle");
assertEquals(getAngleType(180), "Straight angle");
assertEquals(getAngleType(270), "Reflex angle");
assertEquals(getAngleType(390), "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,39 @@
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (denominator === 0) return false;

return Math.abs(numerator) < Math.abs(denominator);
}

// The line below allows us to load the isProperFraction function into tests in other files.
// This will be useful in the "rewrite tests with jest" step.
module.exports = isProperFraction;

// Here's our helper again
// Here's our hel per again
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}
module.exports = isProperFraction;

// 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);
//Test
// Proper fraction with negatives
assertEquals(isProperFraction(7, 9), true);
assertEquals(isProperFraction(-7, 9), true);
assertEquals(isProperFraction(7, -9), true);
assertEquals(isProperFraction(-1, -2), true);
assertEquals(isProperFraction(0, -5), true);

// Improper fractions
assertEquals(isProperFraction(9, -7), false);
assertEquals(isProperFraction(-9, 7), false);
assertEquals(isProperFraction(-9, -7), false);
assertEquals(isProperFraction(17, 3), false);

//Edge case
assertEquals(isProperFraction(1, 0), false);
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,42 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
if (!card || typeof card !== "string") {
throw new Error("Invalid card");
}
const rank = card.slice(0, -1);
const suit = card.slice(-1);

const validSuits = ["♠", "♥", "♦", "♣"];
const validRanks = [
"A",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"J",
"Q",
"K",
];

if (!validSuits.includes(suit) || !validRanks.includes(rank)) {
throw new Error("Invalid card");
}
if (rank === "A") return 11;
if (rank === "J" || rank === "Q" || rank === "K") return 10;

return Number(rank);
}

export default getCardValue;

// The line below allows us to load the getCardValue function into tests in other files.
// This will be useful in the "rewrite tests with jest" step.
module.exports = getCardValue;

// Helper functions to make our assertions easier to read.
function assertEquals(actualOutput, targetOutput) {
Expand All @@ -39,14 +69,32 @@ function assertEquals(actualOutput, targetOutput) {

// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:

// Valid cards
assertEquals(getCardValue("9♠"), 9);
assertEquals(getCardValue("A♠"), 11);
assertEquals(getCardValue("J♣"), 10);
assertEquals(getCardValue("10♥"), 10);

// What other invalid card cases can you think of?

// Handling invalid cards
// Invalid cases
try {
getCardValue("invalid");
console.log("fail invalid");
} catch (e) {
console.log("pass 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?
try {
getCardValue("1♠");
console.log("fail 1♠");
} catch (e) {
console.log("pass 1♠");
}
try {
getCardValue("A");
console.log("fail A");
} catch (e) {
console.log("pass A");
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,29 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

// Case 2: Right angle
test(`should return "Right angle" when angle is 90`, () => {
expect(getAngleType(90)).toEqual("Right angle");
});
// Case 3: Obtuse angles
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
expect(getAngleType(91)).toEqual("Obtuse angle");
expect(getAngleType(135)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
});
// Case 4: Straight angle
test(`should return "Straight angle" when angle is 180`, () => {
expect(getAngleType(180)).toEqual("Straight angle");
});
// Case 5: Reflex angles
test(`should return "Reflex angle" when (180 < angle < 360)`, () => {
expect(getAngleType(181)).toEqual("Reflex angle");
expect(getAngleType(270)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});
// Case 6: Invalid angles
test(`should return "Invalid angle" when (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(361)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,37 @@ 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
// Special case: denominator is zero
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});

// Special case: numerator is zero
test(`should return true when numerator is zero`, () => {
expect(isProperFraction(0, 5)).toEqual(true);
});

// Special case: positive numerator and denominator
test(`should return true when numerator is less than denominator`, () => {
expect(isProperFraction(5, 9)).toEqual(true);
});

// Special case; negative numerator and positive denominatore
test(` should return true when numertaor is negative and less than demoniator`, () => {
expect(isProperFraction(-5, 9)).toEqual(true);
});

// special case: postive numerator and negative deminator
test(`should return true when numerator is positive and less than denominator`, () => {
expect(isProperFraction(5, -9)).toEqual(true);
});

// Special case: negative numerator and negative denominator
test(`should return true when both are negative`, () => {
expect(isProperFraction(-5, -9)).toEqual(true);
});
Comment on lines +18 to +35
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 descriptions are not quite correct without mentioning the comparison is to be made on their absolute value. For example, 5 is actually more than -9, but abs(5) < abs(9).

Note: We can use pseudo-code and notations like abs(...) or | ... | in the descriptions to more
concisely describe the conditions (the "when" part).


// Special case: Equal values
test(`should return false when numerator and denominator are equal`, () => {
expect(isProperFraction(4, 4)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,36 @@ 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`, () => {
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)
test(`should return the correct value for number cards`, () => {
expect(getCardValue("2♠")).toEqual(2);
expect(getCardValue("10♣")).toEqual(10);
expect(getCardValue("6♥")).toEqual(6);
expect(getCardValue("9♦")).toEqual(9);
expect(getCardValue("3♠")).toEqual(3);
});
// Face Cards (J, Q, K)
test(`should return the correct value for face cards`, () => {
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("Q♠")).toEqual(10);
expect(getCardValue("K♠")).toEqual(10);
});
// Invalid Cards
test("should throw error for invalid cards", () => {
expect(() => getCardValue("1♠")).toThrow();
expect(() => getCardValue("11♠")).toThrow();
expect(() => getCardValue("Z♠")).toThrow();
expect(() => getCardValue("A")).toThrow();
expect(() => getCardValue("♠")).toThrow();
expect(() => getCardValue("")).toThrow();
expect(() => getCardValue(null)).toThrow();
});

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