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 @@ -16,6 +16,24 @@

function getAngleType(angle) {
// TODO: Implement this function
if (angle <= 0 || angle >=360) {
return "Invalid angle";
}
if (angle >= 0 && angle < 90) {
return "Acute angle";
}
if (angle === 90) {
return "Right angle";
}
if (angle > 90 && angle < 180) {
return "Obtuse angle";
}
if (angle === 180) {
return "Straight angle";
}
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 @@ -35,3 +53,39 @@ function assertEquals(actualOutput, targetOutput) {
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");

// Acute Angles
let acute = getAngleType(1);
assertEquals(acute, "Acute angle");
acute = getAngleType(45);
assertEquals(acute, "Acute angle");
acute = getAngleType(89);
assertEquals(acute, "Acute angle");

// Obtuse Angles
let obtuse = getAngleType(91);
assertEquals(obtuse, "Obtuse angle");
obtuse = getAngleType(135);
assertEquals(obtuse, "Obtuse angle");
obtuse = getAngleType(179);
assertEquals(obtuse, "Obtuse angle");

// Straight Line Angles
let straight = getAngleType(180);
assertEquals(straight, "Straight angle");

// Reflex Angles
let reflex = getAngleType(181);
assertEquals(reflex, "Reflex angle");
reflex = getAngleType(287);
assertEquals(reflex, "Reflex angle");
reflex = getAngleType(359);
assertEquals(reflex, "Reflex angle");

// Invalid Angles
let invalid = getAngleType(0);
assertEquals(invalid, "Invalid angle");
invalid = getAngleType(360);
assertEquals(invalid, "Invalid angle");
invalid = getAngleType(-20);
assertEquals(invalid, "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (denominator === 0) {
return false;
}
// check if both numbers are integers
if (!Number.isInteger(numerator) || !Number.isInteger(denominator)) {
return false;
}
return Math.abs(numerator) < Math.abs(denominator);
}

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

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(5, 1), false);
assertEquals(isProperFraction(7, 7), false);

//Negative fractions
assertEquals(isProperFraction(-5, -2), false);
assertEquals(isProperFraction(-1, -2), true);
assertEquals(isProperFraction(-5, 3), false);
assertEquals(isProperFraction(1, -2), true);
assertEquals(isProperFraction(4, -3), false);

//0 in Numerator or denominator
assertEquals(isProperFraction(0, -2), true);
assertEquals(isProperFraction(-1, 0), false);
assertEquals(isProperFraction(0, 0), false);

// With decimals
assertEquals(isProperFraction(2.5, -3), false);
assertEquals(isProperFraction(-3.4, 6.3), false);
assertEquals(isProperFraction(-7.4, 3.3), false);

// Boundary cases where |Numerator| equals |Denominator|
assertEquals(isProperFraction(7, -7), false);
assertEquals(isProperFraction(-21, -21), false);
assertEquals(isProperFraction(119, 119), false);
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@

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

const validSuits = ["♠", "♥", "♦", "♣"];
const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];

if (!validSuits.includes(cardSuit) || !validRanks.includes(cardRank)) {
throw new Error("Invalid card");
}

switch (cardRank) {
case "A":
return 11;
case "K":
case "Q":
case "J":
return 10;
default:
return Number(cardRank);
}
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -40,13 +60,44 @@ function assertEquals(actualOutput, targetOutput) {
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:
assertEquals(getCardValue("9♠"), 9);
assertEquals(getCardValue("3♣"), 3);
assertEquals(getCardValue("10♦"), 10);
assertEquals(getCardValue("J♥"), 10);
assertEquals(getCardValue("Q♠"), 10);
assertEquals(getCardValue("K♦"), 10);
assertEquals(getCardValue("A♣"), 11);

// Handling invalid cards
try {
getCardValue("invalid");

// This line will not be reached if an error is thrown as expected
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("01♦");
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) {}
try {
getCardValue("K♠♠");
console.error("Error was not thrown for invalid card");
} catch (e) {}
try {
getCardValue("QQ♣");
console.error("Error was not thrown for invalid card");
} catch (e) {}
try {
getCardValue("♦");
console.error("Error was not thrown for invalid card");
} catch (e) {}
try {
getCardValue("J");
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,30 @@ 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(90.1)).toEqual("Obtuse angle");
expect(getAngleType(135)).toEqual("Obtuse angle");
expect(getAngleType(179.9)).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(180.1)).toEqual("Reflex angle");
expect(getAngleType(271)).toEqual("Reflex angle");
expect(getAngleType(359.9)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test(`should return "Invalid angle" when (angle >= 360 || angle === 0 || angle < 0)`, () => {
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(-25)).toEqual("Invalid angle");
expect(getAngleType(460)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,32 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
// Special case: numerator is zero
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
expect(isProperFraction(0, 0)).toEqual(false);
});

// When numerator is greater than the denominator
test(`should return false when |numerator| >= |denominator|`, () => {
expect(isProperFraction(5, 3)).toEqual(false);
expect(isProperFraction(140, 7)).toEqual(false);
expect(isProperFraction(-16, -5)).toEqual(false);
expect(isProperFraction(-70, 14)).toEqual(false);
expect(isProperFraction(100, -11)).toEqual(false);
expect(isProperFraction(-13, -13)).toEqual(false);
});

// When the numerator or denominator is not an integer
test(`should return false when numerator or denominator is not an integer`, () => {
expect(isProperFraction(-1.5, 2)).toEqual(false);
expect(isProperFraction(2.3, 4)).toEqual(false);
expect(isProperFraction(3.7, 7)).toEqual(false);
expect(isProperFraction(102.8, -103.2)).toEqual(false);
});

// When the fraction is correct
test(`should return true when numerator < denominator`, () => {
expect(isProperFraction(2, 5)).toEqual(true);
expect(isProperFraction(1, -4)).toEqual(true);
expect(isProperFraction(-100, -214)).toEqual(true);
expect(isProperFraction(-1502, 4000)).toEqual(true);
expect(isProperFraction(18, 32)).toEqual(true);
});
Comment on lines +32 to 38
Copy link
Contributor

Choose a reason for hiding this comment

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

Could have also used the | ... | notation in this description.

Copy link
Author

Choose a reason for hiding this comment

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

Sure. i will keep that in mind. Thank you!

Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,39 @@ const getCardValue = require("../implement/3-get-card-value");
// Case 1: Ace (A)
test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
expect(getCardValue("A♥")).toEqual(11);
expect(getCardValue("A♣")).toEqual(11);
});

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
test(`Should return number when given an 2-10 card`, () => {
expect(getCardValue("8♠")).toEqual(8);
expect(getCardValue("5♥")).toEqual(5);
expect(getCardValue("9♣")).toEqual(9);
expect(getCardValue("2♥")).toEqual(2);
expect(getCardValue("7♦")).toEqual(7);
});
// Face Cards (J, Q, K)
test(`Should return 10 when given a face card`, () => {
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("Q♣")).toEqual(10);
expect(getCardValue("K♥")).toEqual(10);
expect(getCardValue("J♦")).toEqual(10);
});

// Invalid Cards
test(`Should return Invalid when given an Invalid card`, () => {
expect(() => getCardValue("11♠")).toThrow("Invalid card");
expect(() => getCardValue("01♥")).toThrow("Invalid card");
expect(() => getCardValue("1♣")).toThrow("Invalid card");
expect(() => getCardValue("K♠♣")).toThrow("Invalid card");
expect(() => getCardValue("QQ♥")).toThrow("Invalid card");
expect(() => getCardValue("")).toThrow("Invalid card");
expect(() => getCardValue("5♡")).toThrow("Invalid card");
expect(() => getCardValue("Q")).toThrow("Invalid 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

Loading