Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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 @@ -16,6 +16,12 @@

function getAngleType(angle) {
// TODO: Implement this function
if (angle > 0 && angle < 90) return "Acute angle";
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

There are quite a lot of conditionals in here. Do you think there is something other than if-else that could make this simpler, cleaner?

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 +41,31 @@ function assertEquals(actualOutput, targetOutput) {
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");
let acute = getAngleType(1);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is very readable, and there is nothing wrong with that. But do you think you need to declare/reassign a variable for each test? How could you make this simpler and less lines of code?

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(200);
assertEquals(reflex, "Reflex angle");
reflex = getAngleType(243);
assertEquals(reflex, "Reflex angle");
reflex = getAngleType(359);
assertEquals(reflex, "Reflex angle");
let invalid = getAngleType(-11);
assertEquals(invalid, "Invalid angle");
invalid = getAngleType(0);
assertEquals(invalid, "Invalid angle");
invalid = getAngleType(360);
assertEquals(invalid, "Invalid angle");
invalid = getAngleType(371);
assertEquals(invalid, "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (Math.abs(numerator) < Math.abs(denominator)) return true;
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 think this could be done in one statement. How do you think it could be done? it'll make the code much simpler as well.

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, since these are meant to be used in a division, do you think there needs to be a check to see if the potential division could cause any errors? According to Maths, one of these numbers MUST not be a certain number, or else the division is undefined or erroneous.
(If I'm being vague that's because otherwise it'll reveal the answer 😳 )

else return false;
}

// 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 +32,9 @@ function assertEquals(actualOutput, targetOutput) {

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(7, 4), false);
assertEquals(isProperFraction(-8, 5), false);
assertEquals(isProperFraction(9, 10), true);
assertEquals(isProperFraction(-3, -6), true);
assertEquals(isProperFraction(15, 11), false);
assertEquals(isProperFraction(-17, -24), true);
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,42 @@
// 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 getCardValue(card) {
// TODO: Implement this function
if (!card || typeof card !== "string") {
throw new Error("Invalid card");
}
//
const validSuits = ["♠", "♥", "♦", "♣"];
const suit = card.slice(-1); //takes the last character of the string.
const rank = card.slice(0, -1); //takes everything except the last character.

if (!validSuits.includes(suit)) {
throw new Error("Invalid card"); // This if statement checks the suit is valid with the Array of suit we assigned
}
// Based on the question i get form mentor i changed the function if statement for better experience of the code.
const cardValues = {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do you think there is a better data type to use for a set of cards? 😉

A: 11,
J: 10,
Q: 10,
K: 10,
2: 2,
3: 3,
4: 4,
5: 5,
6: 6,
7: 7,
8: 8,
9: 9,
10: 10,
}; // This is java script object that act as like lookup table for the valid card value input.

if (cardValues[rank] !== undefined) return cardValues[rank];
throw new Error("Invalid card");
}
Comment thread
cjyuan marked this conversation as resolved.

module.exports = 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,41 @@ const getAngleType = require("../implement/1-get-angle-type");
// including boundary and invalid cases.

// Case 1: Acute angles
test(`should return "Acute angle" when (0 < angle < 90)`, () => {
test(`should return "Acute angles" 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");
expect(getAngleType(1)).toBe("Acute angle");
expect(getAngleType(45)).toBe("Acute angle");
expect(getAngleType(89)).toBe("Acute angle");
});

// Case 2: Right angle
test(`should return "Right angle" when (angle === 90)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(90)).toBe("Right angle");
});
// Case 3: Obtuse angles
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(97)).toBe("Obtuse angle");
expect(getAngleType(129)).toBe("Obtuse angle");
expect(getAngleType(165)).toBe("Obtuse angle");
});
// Case 4: Straight angle
test(`should return "Straight angle" when (angle ==180)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(180)).toBe("Straight angle");
});
// Case 5: Reflex angles
test(`should return "Reflex angles" when (180 < angle < 360)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(191)).toBe("Reflex angle");
expect(getAngleType(250)).toBe("Reflex angle");
expect(getAngleType(317)).toBe("Reflex angle");
});
// Case 6: Invalid angles
test(`should return "Invalid angles" when (angle >= 360 or angle<= 0 )`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(0)).toBe("Invalid angle");
expect(getAngleType(-45)).toBe("Invalid angle");
expect(getAngleType(370)).toBe("Invalid angle");
Comment thread
cjyuan marked this conversation as resolved.
expect(getAngleType(360)).toBe("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,25 @@
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`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
expect(isProperFraction(-11, 0)).toEqual(false);
expect(isProperFraction(0, 0)).toEqual(false);
expect(isProperFraction(7, 0)).toEqual(false);
});
// It should return false when the numerator > the denominator
test("should return true when numerator is 0", () => {
expect(isProperFraction(0, -1)).toEqual(true);
expect(isProperFraction(0, 3)).toEqual(true);
expect(isProperFraction(0, -2)).toEqual(true);
});
test("should return true when abs(numerator) > abs(denominator)", () => {
expect(isProperFraction(-12, -19)).toEqual(true);
expect(isProperFraction(0, 6)).toEqual(true);
expect(isProperFraction(17, -71)).toEqual(true);
});
test("should return false when abs(numerator) < abs(denominator)", () => {
expect(isProperFraction(-180, -109)).toEqual(false);
expect(isProperFraction(27, 5)).toEqual(false);
expect(isProperFraction(-29, 17)).toEqual(false);
});
Comment thread
cjyuan marked this conversation as resolved.
Outdated
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,29 @@ const getCardValue = require("../implement/3-get-card-value");
test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
});

// Case 2: Number Cards (2-10)
test(`Should return the numeric value for number cards`, () => {
expect(getCardValue("2♠")).toEqual(2);
expect(getCardValue("5♥")).toEqual(5);
expect(getCardValue("9♦")).toEqual(9);
expect(getCardValue("10♣")).toEqual(10);
});
// Case 3: Face Cards (J, Q, K)
test(`Should return 10 for face cards`, () => {
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("Q♥")).toEqual(10);
expect(getCardValue("K♦")).toEqual(10);
});
// Case 4: Invalid Cards
test(`Should throw "Invalid card" for invalid cards`, () => {
expect(() => getCardValue("1♠")).toThrow("Invalid card");
expect(() => getCardValue("B♠")).toThrow("Invalid card");
expect(() => getCardValue("A$")).toThrow("Invalid card");
expect(() => getCardValue("10X")).toThrow("Invalid card");
expect(() => getCardValue("0x02♠")).toThrow("Invalid card");
expect(() => getCardValue("2.1♠")).toThrow("Invalid card");
expect(() => getCardValue("0002♠")).toThrow("Invalid card");
});
// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
// Face Cards (J, Q, K)
Expand All @@ -17,4 +39,3 @@ test(`Should return 11 when given an ace card`, () => {
// 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