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 @@ -15,23 +15,83 @@
// 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 == 90) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Whats the difference between == and === and why are you using == here?

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.

== converts and compares operands that are of different types and === always considers operands of different types to be different. In this case, angle == 90 which means LHS is compared with RHS and it is equal. I can also use ===.

return "Right angle";
}
else if (angle == 180) {
return "Straight angle";
}
else if (angle > 0 && angle < 90) {
return "Acute angle";
}
else if (angle > 90 && angle < 180) {
return "Obtuse angle";
}
else
return "Reflex 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}`
);
}
const invalid = getAngleType(0);
assertEquals(invalid, "Invalid angle");
console.log (getAngleType(0));

const invalid0 = getAngleType(-10);
assertEquals(invalid, "Invalid angle");
console.log (getAngleType(-10));

const invalid1 = getAngleType(360);
assertEquals(invalid1, "Invalid angle");
console.log (getAngleType(360));

const invalid2 = getAngleType(900);
assertEquals(invalid2, "Invalid angle");
console.log (getAngleType(900));

// TODO: Write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");
console.log (getAngleType(90));

const straight = getAngleType(180);
assertEquals(straight, "Straight angle");
console.log (getAngleType(180));

const acute = getAngleType(45);
assertEquals(acute, "Acute angle");
console.log (getAngleType(45));

const obtuse = getAngleType(135);
assertEquals(obtuse, "Obtuse angle");
console.log (getAngleType(135));

const reflex = getAngleType(240);
assertEquals(reflex, "Reflex angle");
console.log (getAngleType(240));

module.exports = getAngleType;

// 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");
Comment on lines +83 to +97

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why is there commented out code?

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.

In my file there is no commented code showing. I have attached the screenshot.
Screenshot 2026-07-12 222351

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Did you commit and push all code?
If you check on the remote (for example in the PR diff view on Github) you' ll see the code: https://github.com/CodeYourFuture/Module-Structuring-and-Testing-Data/pull/1470/changes#diff-714a296fc0e0b2cf1e44da9dcbd08ccd678295a69af1884e7f2e1686aaef8626R81

Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,56 @@
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (numerator <= 0 || denominator <= 0) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why does the numerator be bigger than 0?

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.

Sorry I cannot understand your question. It says, the numerator is less than or equal to 0.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Sorry I was missing a word. Why can the numerator not be 0?

return false;
}
return numerator < 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}`
);
}

const case1 = isProperFraction(1, 2);
assertEquals(case1, true);
console.log(isProperFraction(1, 2));

const case2 = isProperFraction(3, 4);
assertEquals(case2, true);
console.log(isProperFraction(3, 4));

const case3 = isProperFraction(4, 4);
assertEquals(case3, false);
console.log(isProperFraction(4, 4));

const case4 = isProperFraction(8, 5);
assertEquals(case4, false);
console.log(isProperFraction(8, 5));

const case5 = isProperFraction(16, 22);
assertEquals(case5, true);
console.log(isProperFraction(16, 22));

const case6 = isProperFraction(0, 6);
assertEquals(case6, false);
console.log(isProperFraction(0, 6));

const case7 = isProperFraction(5, 0);
assertEquals(case7, false);
console.log(isProperFraction(5, 0));

module.exports = isProperFraction;

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


// Here's our helper again
// 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(1, 2), true);
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,92 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
}
const rank = card.slice(0, -1);
const suit = card.slice(-1);

const validRank = ["A","2","3","4","5","6","7","8","9","10","J","Q","K"];
const validSuit = ["♠","♥","♦","♣"];
Comment on lines +28 to +29

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good job on explicitly listing all allowed ranks and suits. This makes the code easier to read and the logic for checking easier as well.

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.

Thank you!


if (!validRank.includes(rank) || !validSuit.includes(suit)) {
throw new Error("Invalid Card");
}

if (rank === "A") {
return 11;
}
else if (rank === "J" || rank=== "Q" || rank === "K") {
return 10;
}

// 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;
else if (rank >= 2 && rank <= 10) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What happens if the condition is false?

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.

If rank >= 2 && rank <= 10 is false, suppose if rank = 11, the function does not return Number(rank). For invalid rank throw new Error("Invalid Card") check before reaching this point.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

So when the code reaches this point you can already be certain that the rank is between 2 and 10. You did the checks earlier so this condition is not needed and can be removed.

return Number(rank);
}
Comment on lines +31 to +44

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The indentation is a bit off. How can you ensure consistent formatting in your code?

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 used prettier tool to fix the formation of the entire code. Please let me know if it is okay or not.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I don't see any changes on this file. Did you push the changes?

}

// Helper functions to make our assertions easier to read.
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}

// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:
assertEquals(getCardValue("9♠"), 9);
const ace = getCardValue("A♠");
assertEquals(getCardValue("A♠"), 11);
console.log (getCardValue("A♠"));

const faceJ = getCardValue("J♣");
assertEquals(getCardValue("J♣"), 10);
console.log (getCardValue("J♣"));

const faceQ = getCardValue("Q♦");
assertEquals(getCardValue("Q♦"), 10);
console.log (getCardValue("Q♦"));

const faceK = getCardValue("K♦");
assertEquals(getCardValue("K♦"), 10);
console.log (getCardValue("K♦"));

const number5 = getCardValue("5♥");
assertEquals(getCardValue("5♥"), 5);
console.log (getCardValue("5♥"));

const number10 = getCardValue("10♥");
assertEquals(getCardValue("10♥"), 10);
console.log (getCardValue("10♥"));

// Handling invalid cards
try {
getCardValue("invalid");
getCardValue("10");
console.error("Error was not thrown for invalid card 😢");
} catch (e) {
console.log("Error thrown for invalid card 🎉");
}

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

try {
getCardValue("10x");
console.error("Error was not thrown for invalid card 😢");
} catch (e) {
console.log("Error thrown for invalid card 🎉");
}

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

try {
getCardValue("Qxx");
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 🎉");
}

// What other invalid card cases can you think of?
module.exports = getCardValue;
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
// 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.

// 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(`should return "Right angle" when (angle == 90)`, () => {
expect(getAngleType(90)).toEqual("Right angle");
});
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
expect(getAngleType(91)).toEqual("Obtuse angle");
expect(getAngleType(120)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
});
test(`should return "Straight angle" when (angle == 180)`, () => {
expect(getAngleType(180)).toEqual("Straight angle");
});
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");
Comment on lines +12 to +22

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good job on testing the border cases here

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.

Thank you!

});
test(`should return "Invalid angle" when (angle <= 0 || angle >= 360)`, () => {
expect(getAngleType(-1)).toEqual("Invalid angle");
expect(getAngleType(370)).toEqual("Invalid angle");
Comment on lines +25 to +26

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Which border cases can you test here? (Which numbers are closest to the condition?)

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 tested for the invalid case when (angle <= 0 || angle >= 360). So, when i considered -1 and 370, the closest number for this condition is -1 and 361. Because -1 is closest to 0 and 361 is closest to 360.

});

// Case 2: Right angle
// Case 3: Obtuse angles
// Case 4: Straight angle
// Case 5: Reflex angles
// Case 6: Invalid angles
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,21 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});
test(`should return true when the function is proper fraction`, () => {
expect(isProperFraction(1, 2)).toEqual(true);
});
test(`should return false when the function is improper fraction`, () => {
expect(isProperFraction(8, 5)).toEqual(false);
});
test(`should return false when the numerator is equal to denominator`, () => {
expect(isProperFraction(4, 4)).toEqual(false);
});
test(`should return false when numerator is zero`, () => {
expect(isProperFraction(0, 1)).toEqual(false);
});
test(`should return false when numerator is negative`, () => {
expect(isProperFraction(-1, 1)).toEqual(false);
});
test(`should return false when denominator is negative`, () => {
expect(isProperFraction(1, -1)).toEqual(false);
});
Comment on lines +23 to +28

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What about values like -1/2 or 1/-2. What should the function return for them?

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 think for values like -1/2 or 1/-2, the function will also return false because proper fractions normally require positive numerator and denominator.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I believe a proper fraction can also include negative values
In general, a common fraction is said to be a proper fraction if the absolute value of the fraction is strictly less than one—that is, if the fraction is greater than −1 and less than 1.

Original file line number Diff line number Diff line change
@@ -1,20 +1,50 @@
// This statement loads the getCardValue function you wrote in the implement directory.
// 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`, () => {
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

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

describe ("Number Cards" , () => {
test(`Should return 5 for 5♥`, () => {
expect(getCardValue("5♥")).toEqual(5);
});
test(`Should return 10 for 10♥`, () => {
expect(getCardValue("10♥")).toEqual(10);
});
});
describe ("Face Cards" , () => {
test(`Should return 10 for J♣`, () => {
expect(getCardValue("J♣")).toEqual(10);
});
test(`Should return 10 for Q♦`, () => {
expect(getCardValue("Q♦")).toEqual(10);
});
test(`Should return 10 for K♦`, () => {
expect(getCardValue("K♦")).toEqual(10);
});
});
describe("Invalid Cards", () => {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Which part of the condition is not covered by the invalid test cases yet?
if (!validRank.includes(rank) || !validSuit.includes(suit)) {

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.

Is it for Invalid suit?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Yes exactly. Please add some test cases for verifying teh valid suit check works correctly

test("throws error for invalid card", () => {
expect(() => {
getCardValue("10");
}).toThrow();
});
test("throws error for invalid card", () => {
expect(() => {
getCardValue("A");
}).toThrow();
});
test("throws error for invalid card", () => {
expect(() => {
getCardValue("10x");
}).toThrow();
});
test("throws error for invalid card", () => {
expect(() => {
getCardValue("JK");
}).toThrow();
});
test("throws error for invalid card", () => {
expect(() => {
getCardValue("Qxx");
}).toThrow();
});
});
Loading