Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
// 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 (90 < angle && angle < 180) return "Obtuse angle";
else if (angle === 180) return "Straight angle";
else if (180 < angle && 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 @@ -35,3 +40,34 @@ function assertEquals(actualOutput, targetOutput) {
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");
let acute = getAngleType(1);
assertEquals(acute, "Acute angle");
acute = getAngleType(45);
assertEquals(acute, "Acute angle");
acute = getAngleType(89);
assertEquals(acute, "Acute angle");
let obtuse = getAngleType(91);
assertEquals(obtuse, "Obtuse angle");
obtuse = getAngleType(100);
assertEquals(obtuse, "Obtuse angle");
obtuse = getAngleType(179);
assertEquals(obtuse, "Obtuse angle");
const straight = getAngleType(180);
assertEquals(straight, "Straight angle");
let reflex = getAngleType(181);
assertEquals(reflex, "Reflex angle");
reflex = getAngleType(200);
assertEquals(reflex, "Reflex angle");
reflex = getAngleType(359);
assertEquals(reflex, "Reflex angle");
let invalid = getAngleType(-1);
assertEquals(invalid, "Invalid angle");
invalid = getAngleType(0);
assertEquals(invalid, "Invalid angle");
invalid = getAngleType(360);
assertEquals(invalid, "Invalid angle");
invalid = getAngleType(361);
assertEquals(invalid, "Invalid angle");



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

function isProperFraction(numerator, denominator) {
Comment thread
Elisabeth-Matulian marked this conversation as resolved.
// TODO: Implement this function
if (denominator === 0) {
return false;
}
else {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

An else statement is technically not required here (given the if statement above would return if the condition was fulfilled, and this is the final case before the function ends) but it's not a breaking change

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.

So, is it always possible not to use else?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Technically, yes, but it depends on the flow of your program and what you want to achieve.

Doing something like this in a function:

{
if(name === 'Bob') { return name }
return 'Not found'
}

Will always fall to the final return if the condition is not met. If, however, you had multiple conditions or more code that you wanted/not wanted to execute after the else statement, you could use it as a case that catches all possible branches.

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;
Expand All @@ -31,3 +36,13 @@ function assertEquals(actualOutput, targetOutput) {

// Example: 1/2 is a proper fraction
Comment thread
Elisabeth-Matulian marked this conversation as resolved.
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(7, 1), false);
assertEquals(isProperFraction(0, 5), true);
assertEquals(isProperFraction(-1, -2), true);
assertEquals(isProperFraction(-2, -1), false);
assertEquals(isProperFraction(-2, 5), true);
assertEquals(isProperFraction(5, -2), false);
assertEquals(isProperFraction(5, 5), false);
assertEquals(isProperFraction(5, 0), false);
assertEquals(isProperFraction(-2, 0), false);
``
Comment thread
Elisabeth-Matulian marked this conversation as resolved.
Outdated
Comment thread
Elisabeth-Matulian marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
Comment thread
Elisabeth-Matulian marked this conversation as resolved.
// TODO: Implement this function
const cardNum = card.slice(0, -1)
const cardSuit = card.slice(-1)
if (cardNum === "A" && "♠♥♦♣".includes(cardSuit)) { return 11}
else if (["J", "Q", "K"].includes(cardNum)) { return 10}
else if (Number(cardNum) >= 2 && Number(cardNum) <= 10) {return Number(cardNum)}
else throw new Error("Invalid card");
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -40,13 +45,34 @@ 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("2♥"), 2);
assertEquals(getCardValue("10♥"), 10);
assertEquals(getCardValue("Q♦"), 10);
// Handling invalid cards
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");
} catch (e) {}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Let's make the catch blocks a bit more interesting. How can we assert the error content against what we would expect the function to throw?

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 need to somehow implement the assertEquals function in try catch, but I haven't found any examples.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

A little hint, what would happen if you temporarily add a console.log(e)? Can you use anything to validate the content of your error?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Also, for javascript I highly recommend checking MDN for documentation and references. You might find this one useful to find your answer: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#examples

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.

This took a while for me but was pretty helpful. Thanks. Looks like it works

try {
getCardValue("♦Q");

// This line will not be reached if an error is thrown as expected
console.error("Error was not thrown for invalid card");
} catch (e) {}
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) {}

try {
getCardValue("AX");

// 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?
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,31 @@ 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 (90 < angle < 180)`, () => {
expect(getAngleType(91)).toEqual("Obtuse angle");
expect(getAngleType(100)).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 (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 (360 <= angle or angle <= 0)`, () => {
expect(getAngleType(-1)).toEqual("Invalid angle");
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(361)).toEqual("Invalid angle");
});


Comment thread
Elisabeth-Matulian marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,36 @@ 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(`should return false when denominator is 0`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});
test(`should return true when numerator is less than denominator`, () => {
expect(isProperFraction(1, 2)).toEqual(true);
});
test(`should return false when numerator is greater than denominator`, () => {
expect(isProperFraction(7, 1)).toEqual(false);
});
test(`should return true when numerator is 0`, () => {
expect(isProperFraction(0, 5)).toEqual(true);
});
test(`should return true when numerator and denominator are negative`, () => {
expect(isProperFraction(-1, -2)).toEqual(true);
});
test(`should return false when numerator and denominator are negative and numerator is greater than denominator`, () => {
expect(isProperFraction(-2, -1)).toEqual(false);
});
test(`should return true when numerator is negative and denominator is positive`, () => {
expect(isProperFraction(-2, 5)).toEqual(true);
});
test(`should return false when numerator is positive and denominator is negative`, () => {
expect(isProperFraction(5, -2)).toEqual(false);
});
test(`should return false when numerator equals denominator`, () => {
expect(isProperFraction(5, 5)).toEqual(false);
});
test(`should return false when denominator is 0`, () => {
expect(isProperFraction(5, 0)).toEqual(false);
});
test(`should return false when denominator is 0`, () => {
expect(isProperFraction(-2, 0)).toEqual(false);
});
Comment thread
Elisabeth-Matulian marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@ const getCardValue = require("../implement/3-get-card-value");
test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
});
test(`Should return 9 when given an 9♠`, () => {
expect(getCardValue("9♠")).toEqual(9);
});
test(`Should return 2 when given an 2♥`, () => {
expect(getCardValue("2♥")).toEqual(2);
});
test(`Should return 10 when given an 10♥`, () => {
expect(getCardValue("10♥")).toEqual(10);
});
test(`Should return 10 when given an Q♦`, () => {
expect(getCardValue("Q♦")).toEqual(10);
});
test(`Should return Error when given an ♦Q`, () => {
expect(function() {getCardValue("♦Q");}).toThrow("Invalid card");
});
test(`Should return Error when given an 11♦`, () => {
expect(function() {getCardValue("11♦")}).toThrow("Invalid card");
});
test(`Should return Error when given an AX`, () => {
expect(function() {getCardValue("11♦")}).toThrow("Invalid card");
});
Comment thread
Elisabeth-Matulian marked this conversation as resolved.
Outdated


// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
Expand Down