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,21 @@

function getAngleType(angle) {
// TODO: Implement this function
let returnAngle;
if (angle > 0 && angle < 90) {
returnAngle = "Acute";
} else if (angle === 90) {
returnAngle = "Right";
} else if (angle > 90 && angle < 180) {
returnAngle = "Obtuse";
} else if (angle === 180) {
returnAngle = "Straight";
} else if (angle > 180 && angle < 360) {
returnAngle = "Reflex";
} else {
returnAngle = "Invalid";
}
return `${returnAngle} angle`;
}

// The line below allows us to load the getAngleType function into tests in other files.
Expand All @@ -34,4 +49,17 @@ function assertEquals(actualOutput, targetOutput) {
// TODO: Write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles
const right = getAngleType(90);
const acute = getAngleType(75);
const obtuse = getAngleType(150);
const straight = getAngleType(180);
const reflex = getAngleType(340);
let invalid = getAngleType(0);
invalid = getAngleType(360);
invalid = getAngleType(-2);

assertEquals(right, "Right angle");
assertEquals(acute, "Acute angle");
assertEquals(obtuse, "Obtuse angle");
assertEquals(straight, "Straight angle");
assertEquals(reflex, "Reflex angle");
assertEquals(invalid, "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (denominator === 0) 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 +33,9 @@ function assertEquals(actualOutput, targetOutput) {

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(3, 1), false);
assertEquals(isProperFraction(2, 2), false);
assertEquals(isProperFraction(5, 0), false);
assertEquals(isProperFraction(-1, 0), false);
assertEquals(isProperFraction(-2, 4), true);
assertEquals(isProperFraction(4, -2), false);
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@

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

// Throw error if card is empty
if (!card) throw new Error(`Invalid card`);

const cardSuit = card[card.length - 1];
// Throw error is card suit is not valid
if (!cardSuits.includes(cardSuit)) {
throw new Error(`Invalid card suit`);
}

const cardRank = card.slice(0, -1);
// Throw error is cardRank is not valid
if (!cardRanks.includes(cardRank)) {
throw new Error(`Invalid card rank`);
}

// Return the card value based on given rank
if (cardRank === "A") return 11;
if (["J", "Q", "K"].includes(cardRank)) return 10;
return Number(cardRank);
}

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

// Handling invalid cards
try {
getCardValue("invalid");
function assertErrors(func, errorMessage) {
try {
func();
console.error("Error was not thrown for invalid card 😢");
} catch (error) {
console.assert(
error.message.includes(errorMessage),
`Expected error: ${errorMessage}, got: ${error.message}`
);
console.log("Error thrown for invalid card 🎉");
}
}

function testInvalidCards() {
const invalidCardCases = [
["A", "Invalid card suit"],
["11♠", "Invalid card rank"],
["N♠", "Invalid card rank"],
["7K", "Invalid card suit"],
["", "Invalid card"],
];

// This line will not be reached if an error is thrown as expected
console.error("Error was not thrown for invalid card 😢");
} catch (e) {
console.log("Error thrown for invalid card 🎉");
invalidCardCases.forEach(([card, message]) => {
console.log("-".repeat(50));
console.log(`Card: ${card}, Message: ${message}`);
assertErrors(() => getCardValue(card), message);
});
}

// What other invalid card cases can you think of?
testInvalidCards();
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,28 @@ 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(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 (180 < angle < 360)`, () => {
expect(getAngleType(181)).toEqual("Reflex angle");
expect(getAngleType(340)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});
// Case 6: Invalid angles
test(`should return "Invalid" when (angle < 1 || angel > 360)`, () => {
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(-15)).toEqual("Invalid angle");
expect(getAngleType(361)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,13 @@ 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 false when (numerator >= denominator)`, () => {
expect(isProperFraction(2, 1)).toEqual(false);
expect(isProperFraction(2, 2)).toEqual(false);
});

test(`should treat numerator and denominator as absolute values when determining proper fractions`, () => {
expect(isProperFraction(-2, 3)).toEqual(true);
expect(isProperFraction(-7, 2)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,31 @@ test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
});

test(`Should return 10 when given an a face card of J, Q or K`, () => {
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("Q♠")).toEqual(10);
expect(getCardValue("K♠")).toEqual(10);
});

test(`Should return number when given an a face card (>= 2 && < 10)`, () => {
expect(getCardValue("2♠")).toEqual(2);
expect(getCardValue("6♠")).toEqual(6);
expect(getCardValue("9♠")).toEqual(9);
});

// Invalid cards
test(`Should return "Invalid card suit" when suit is invalid`, () => {
expect(() => getCardValue("A")).toThrowError();
});

test(`Should return "Invalid card rank" when rank is invalid`, () => {
expect(() => getCardValue("1♠")).toThrowError();
});

test(`Should return "Invalid card" when rank is an empty card`, () => {
expect(() => getCardValue("")).toThrowError();
});

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
// Face Cards (J, Q, K)
Expand All @@ -17,4 +42,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

15 changes: 0 additions & 15 deletions package.json

This file was deleted.