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
@@ -1,3 +1,4 @@

// Implement a function getAngleType
//
// When given an angle in degrees, it should return a string indicating the type of angle:
Expand All @@ -13,25 +14,35 @@
// Acceptance criteria:
// After you have implemented the function, write tests to cover all the cases, and
// 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.
// 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
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${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(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(-10), "invalid angle");
assertEquals(getAngleType(400), "invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,35 @@
// 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
function assertEquals(actualOutput, targetOutput) {
console.assert(
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
// Proper fractions
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(0, 5), true);
assertEquals(isProperFraction(-1, 2), true);
assertEquals(isProperFraction(1, -2), true);
assertEquals(isProperFraction(-1, -2), true);

// Improper fractions
assertEquals(isProperFraction(4, 3), false);
assertEquals(isProperFraction(6, 5), false);
assertEquals(isProperFraction(-4, 3), false);
assertEquals(isProperFraction(4, -3), false);
assertEquals(isProperFraction(-4, -3), false);

// Zero denominator
assertEquals(isProperFraction(5, 0), false);
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,23 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
const rank = card.slice(0, -1); // everything except the last character (suit)
const validSuits = ["♠", "♥", "♦", "♣"];
const suit = card.slice(-1);

if (!validSuits.includes(suit)) {
throw new Error("Invalid card");
}

if (rank === "A") {
return 11;
} else if (rank === "J" || rank === "Q" || rank === "K") {
return 10;
} else if (["2","3","4","5","6","7","8","9","10"].includes(rank)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

is it possible to simplify this else if statement using Comparison Operators?

return Number(rank);
} else {
throw new Error("Invalid card");
}
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -46,9 +62,37 @@ 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 😢");
console.error("Error was not thrown for invalid card ");
} catch (e) {
console.log("Error thrown for invalid card 🎉");
console.log("Error thrown for invalid card ");
}

assertEquals(getCardValue("A♠"), 11);
assertEquals(getCardValue("J♣"), 10);
assertEquals(getCardValue("Q♦"), 10);
assertEquals(getCardValue("K♥"), 10);
assertEquals(getCardValue("2♠"), 2);
assertEquals(getCardValue("3♥"), 3);

// What other valid card cases can you think of?
function assertEqual(func, message) {
try {
func();
console.log("Valid card test passed: " + message);
} catch (e) {
console.error("Valid card test failed: " + message + " — " + e.message);
}
}
// What other invalid card cases can you think of?
function assertThrows(func, errorMessage) {
try {
func();
console.error("Error was not thrown ");
} catch (e) {
if (e.message === errorMessage) {
console.log("Error thrown as expected ");
} else {
console.error(`Unexpected error message: ${e.message}`);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
// This statement loads the getAngleType function you wrote in the implement directory.
// We will use the same function, but write tests for it using Jest in this file.
const getAngleType = require("../implement/1-get-angle-type");

// TODO: Write tests in Jest syntax to cover all cases/outcomes,
// including boundary and invalid cases.
test("returns acute angle for angles between 0 and 90", () => {
expect(getAngleType(1)).toEqual("acute angle");
expect(getAngleType(45)).toEqual("acute angle");
expect(getAngleType(89)).toEqual("acute angle");
});

test("returns right angle for 90 degrees", () => {
expect(getAngleType(90)).toEqual("right angle");
});

test("returns obtuse angle for angles between 90 and 180", () => {
expect(getAngleType(91)).toEqual("obtuse angle");
expect(getAngleType(125)).toEqual("obtuse angle");
expect(getAngleType(172)).toEqual("obtuse angle");
});

test("returns straight angle for 180 degrees", () => {
expect(getAngleType(180)).toEqual("straight angle");
});

// Case 1: Acute angles
test(`should return "Acute angle" when (0 < angle < 90)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(1)).toEqual("Acute angle");
expect(getAngleType(45)).toEqual("Acute angle");
expect(getAngleType(89)).toEqual("Acute angle");
test("returns reflex angle for angles between 180 and 360", () => {
expect(getAngleType(181)).toEqual("reflex angle");
expect(getAngleType(270)).toEqual("reflex angle");
expect(getAngleType(359)).toEqual("reflex angle");
});

// Case 2: Right angle
// Case 3: Obtuse angles
// Case 4: Straight angle
// Case 5: Reflex angles
// Case 6: Invalid angles
test("returns invalid angle for angles outside the valid range", () => {
expect(getAngleType(-1)).toEqual("invalid angle");
expect(getAngleType(361)).toEqual("invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,38 @@
// We will use the same function, but write tests for it using Jest in this file.
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`, () => {
test("returns false when denominator is zero", () => {
expect(isProperFraction(1, 0)).toEqual(false);
});

test("returns true for a positive proper fraction", () => {
expect(isProperFraction(1, 2)).toEqual(true);
});

test("returns true when numerator is negative and denominator is positive", () => {
expect(isProperFraction(-1, 2)).toEqual(true);
});

test("returns true when numerator is positive and denominator is negative", () => {
expect(isProperFraction(1, -2)).toEqual(true);
});

test("returns true when both numerator and denominator are negative", () => {
expect(isProperFraction(-1, -2)).toEqual(true);
});

test("returns true when numerator is zero and denominator is non-zero", () => {
expect(isProperFraction(0, 5)).toEqual(true);
});

test("returns false when numerator and denominator are equal", () => {
expect(isProperFraction(2, 2)).toEqual(false);
});

test("returns false when numerator is greater than denominator", () => {
expect(isProperFraction(3, 2)).toEqual(false);
});

test("returns false for a negative improper fraction", () => {
expect(isProperFraction(-3, 2)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,27 @@
// We will use the same function, but write tests for it using Jest in this file.
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("returns 11 for an ace", () => {
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
test("returns 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
test("returns the correct value for number cards", () => {
expect(getCardValue("2♠")).toEqual(2);
expect(getCardValue("5♥")).toEqual(5);
expect(getCardValue("10♦")).toEqual(10);
});

test("throws an error for invalid cards", () => {
expect(() => getCardValue("invalid")).toThrow("Invalid card");
expect(() => getCardValue("")).toThrow("Invalid card");
expect(() => getCardValue("1♠")).toThrow("Invalid card");
expect(() => getCardValue("11♠")).toThrow("Invalid card");
expect(() => getCardValue("A")).toThrow("Invalid card");
expect(() => getCardValue("X♠")).toThrow("Invalid card");
});
Loading