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,7 +15,19 @@
// 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) {
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 if (angle > 180 && angle < 360) {
return "Reflex angle";
}
}

// The line below allows us to load the getAngleType function into tests in other files.
Expand All @@ -31,7 +43,30 @@ 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");
// Invalid angles
assertEquals(getAngleType(0), "Invalid angle");
assertEquals(getAngleType(-10), "Invalid angle");
assertEquals(getAngleType(360), "Invalid angle");

// Acute angles and its boundaries
assertEquals(getAngleType(1), "Acute angle");
assertEquals(getAngleType(45), "Acute angle");
assertEquals(getAngleType(89), "Acute angle");

// Right angle
assertEquals(getAngleType(90), "Right angle");

// Obtuse angles and its boundaries
assertEquals(getAngleType(91), "Obtuse angle");
assertEquals(getAngleType(120), "Obtuse angle");
assertEquals(getAngleType(179), "Obtuse angle");

// Straight angle
assertEquals(getAngleType(180), "Straight angle");

// Reflex angles and its boundaries
assertEquals(getAngleType(181), "Reflex angle");
assertEquals(getAngleType(200), "Reflex angle");
assertEquals(getAngleType(359), "Reflex angle");


Copy link

Choose a reason for hiding this comment

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

I think you should check the definition of a proper fraction again and see if your test case for negative numbers is correct expecially -3/8. I will advise you to use Match.abs()

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for the feedback! My implementation checks whether the value of the fraction is between 0 and 1, which is the standard mathematical definition of a proper fraction. Based on that definition:

-3/8 → -0.375 → not proper

-8/-3 → 2.66… → not proper

-3/-8 → 0.375 → proper

Using Math.abs() would treat negative fractions like -3/8 as proper, even though their value is negative. That’s why I didn’t use Math.abs(), and why my tests reflect the “between 0 and 1” definition. Happy to adjust if a different definition is expected.

Copy link

Choose a reason for hiding this comment

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

The first rule of a proper fraction is that the numerator is less than the denominator. Your solution will fail on most negative numbers. Reason why a proper fraction is when the absolute value of the numerator is less than the absolute value of the denominator. A proper fraction is always less than 1

Based on your code, let's consider the following examples
you are returning properFraction > 0 && properFraction < 1

-1/2 will return false because the division is -0.5, and it is not greater than 0. But this is wrong as the result is less than 1 and the first rule of numerator < denominator stands.

Same thing with -3/8.

You can check this for further info https://www.splashlearn.com/math-vocabulary/proper-fraction

Math.abs() helps you work with negative numbers.

You can do more research on it.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for explaining! I understand now that you're using the definition based on absolute values. I’ll update my code and tests to use Math.abs() so it matches that approach.

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

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
const properFraction = numerator / denominator;
return properFraction > 0 && properFraction < 1;
}

// The line below allows us to load the isProperFraction function into tests in other files.
Expand All @@ -26,8 +27,27 @@ function assertEquals(actualOutput, targetOutput) {
);
}

// TODO: Write tests to cover all cases.
// What combinations of numerators and denominators should you test?

// Example: 1/2 is a proper fraction
// TEST CASES
// Normal Scenarios
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(5, 10), true);

// Boundary Values
assertEquals(isProperFraction(0, 5), false);
assertEquals(isProperFraction(1, 1), false);

// Improper Fraction
assertEquals(isProperFraction(8, 3), false)

// Negative Values
assertEquals(isProperFraction(-3, 8), false);
assertEquals(isProperFraction(-8, -3), false);
assertEquals(isProperFraction(-3, -8), true);

//Very Small
assertEquals(isProperFraction(0.0001, 999999), true);

//Very Large
assertEquals(isProperFraction(999999, 0.0001), false);

Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,48 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
const rankList = [
"A",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"J",
"Q",
"K",
];
const suitList = ["♠", "♥", "♦", "♣"];

const faceCard = rankList.slice(-3);

const cardRank = card.slice(0, -1);
const cardSuit = card.charAt(card.length - 1);

const validCard = rankList.includes(cardRank) && suitList.includes(cardSuit);

if (!validCard) {
throw new Error("Invalid card");
}

if (cardRank === "A") {
return 11;
}

if (faceCard.includes(cardRank)) {
return 10;
}

return Number(cardRank);
}

// The line below allows us to load the getCardValue function into tests in other files.
console.log(getCardValue("J♥"));

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

Expand All @@ -37,9 +75,11 @@ function assertEquals(actualOutput, targetOutput) {
);
}

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

assertEquals(getCardValue("9♠"), 9);
assertEquals(getCardValue("A♥"), 11);
assertEquals(getCardValue("K♦"), 10);
assertEquals(getCardValue("10♣"), 10);

// Handling invalid cards
try {
Expand All @@ -49,4 +89,19 @@ try {
console.error("Error was not thrown for invalid card");
} catch (e) {}


// What other invalid card cases can you think of?
try {
getCardValue("♠9");
console.error("Error was not thrown for invalid card");
} catch (e) {}

try {
getCardValue("1♠");
console.error("Error was not thrown for invalid card");
} catch (e) {}

try {
getCardValue("♠");
console.error("Error was not thrown for invalid card");
} catch (e) {}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 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,
//Write tests in Jest syntax to cover all cases/outcomes,
// including boundary and invalid cases.

// Case 1: Acute angles
Expand All @@ -14,7 +14,33 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

// Case 2: Right angle
test('should return "Right angle" when (angle === 90)', () => {
expect(getAngleType(90)).toEqual("Right angle");
});

// Case 3: Obtuse angles
test('should return "Obtuse angle" when (angle > 90 && angle < 180)', () => {
expect(getAngleType(91)).toEqual("Obtuse angle");
expect(getAngleType(120)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
});

// Case 4: Straight angle
test('should return "Straight angle" when (angle === 180)', () => {
expect(getAngleType(180)).toEqual("Straight angle");

});

// Case 5: Reflex angles
test('should return "Reflex angle" when (angle > 180 && angle < 360)', () => {
expect(getAngleType(181)).toEqual("Reflex angle");
expect(getAngleType(200)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test('should return "Invalid angle" when (angle <= 0 || angle >= 360)', () => {
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(-10)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,35 @@
// 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.
// 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`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});

// Normal case:
test(`should return true in normal scenario`, () => {
expect(isProperFraction(5, 10)).toEqual(true);
});
// Boundary Value
test(`should return false when numerator is 0`, () => {
expect(isProperFraction(0, 5)).toEqual(false);
});
// Improper Fraction
test(`should return false when numerator is greater than denominator`, () => {
expect(isProperFraction(8, 3)).toEqual(false);
});
// Negative Value
test(`should return false when numerator is negative`, () => {
expect(isProperFraction(-3, 8)).toEqual(false);

});
// Very Small
test(`should return true with very small values`, () => {
expect(isProperFraction(0.0001, 999999)).toEqual(true);
});
// very large
test(`should return false with very large`, () => {
expect(isProperFraction(999999, 0.0001)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 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.
// Write tests in Jest syntax to cover all possible outcomes.

// Case 1: Ace (A)
test(`Should return 11 when given an ace card`, () => {
Expand All @@ -11,8 +11,24 @@ test(`Should return 11 when given an ace card`, () => {

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
test(`Should return should return its numeric value`, () => {
expect(getCardValue("9♠")).toEqual(9);
});
// Face Cards (J, Q, K)
test(`Should return 10`, () => {
expect(getCardValue("K♠")).toEqual(10);
});
// Invalid Cards
test(`Should throw an error`, () => {
expect(() => {
getCardValue("♠9");
}).toThrow();
Copy link

Choose a reason for hiding this comment

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

This is good. To take it further, add a test to see if the returned error message is what you are expecting.

Copy link
Author

Choose a reason for hiding this comment

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

Thank you for the feedback . Would add further test to see what error messages they return.

});
test(`Should throw an error`, () => {
expect(() => {
getCardValue("♠");
}).toThrow();
});

// To learn how to test whether a function throws an error as expected in Jest,
// please refer to the Jest documentation:
Expand Down
Loading