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,21 @@
// execute the code to ensure all tests pass.

function getAngleType(angle) {
// Returns the type of angle based on the number of degrees given.
// TODO: Implement this function
if (angle > 0 && angle < 90) {
return "Acute angle";
} else if (angle === 90) {
return "Right angle";
} else if (angle > 90 && angle < 180) {
return "Obtuse angle";
} else if (angle === 180) {
return "Straight angle";
} else if (angle > 180 && 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 +49,37 @@ function assertEquals(actualOutput, targetOutput) {
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");

// Case 2: Identify Acute Angles
// When the angle is between 0 and 90 degrees,
// then should return "Acute angle"
const acute = getAngleType(45);
assertEquals(acute, "Acute angle");

// Case 3: Identify Obtuse Angles
// When the angle is between 90 and 180 degrees,
// then should return "Obtuse angle"
const obtuse = getAngleType(135);
assertEquals(obtuse, "Obtuse angle");

// Case 4: Identify Straight Angles
// When the angle is exactly 180 degrees,
// then should return "Straight angle"
const straight = getAngleType(180);
assertEquals(straight, "Straight angle");

// Case 5: Identify Reflex Angles
// When the angle is between 180 and 360 degrees,
// then should return "Reflex angle"
const reflex = getAngleType(270);
assertEquals(reflex, "Reflex angle");

// Case 6: Identify Invalid Angles
// When the angle is negative or 360+
// then should return "Invalid angle"

const invalid1 = getAngleType(-30);
assertEquals(invalid1, "Invalid angle");

const invalid2 = getAngleType(400);
assertEquals(invalid2, "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (denominator <= 0) {
return false; // not a valid fraction
}
if (numerator < denominator && numerator >= 0) {
return true;
}
return false;
}

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

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

// Example: 3/5 is a postive proper fraction
const properFraction = isProperFraction(3, 5);
assertEquals(properFraction, true);

// Example: 0/4 is a proper fraction with zero numerator
// Zero numerator is allowed in proper fractions
const zeroNumerator = isProperFraction(0, 4);
assertEquals(zeroNumerator, true);

// Example: 7/7 is an improper fraction,
// numerator equals denominator
const equalFraction = isProperFraction(7, 7);
assertEquals(equalFraction, false);

// Example: 8/3 is an improper fraction,
// numerator is larger
const numeratorLarger = isProperFraction(8, 3);
assertEquals(numeratorLarger, false);

// Example: 5/0 is an invalid fraction
// Division by zero is not a valid fraction
const zeroDenominator = isProperFraction(5, 0);
assertEquals(zeroDenominator, false);

// Example: 2/-6 is an invalid fraction
// Negative denominator makes fraction invalid
const negativeDenominator = isProperFraction(2, -6);
assertEquals(negativeDenominator, false);

// Example: -1/4 is an invalid fraction
// Negative numerator makes fraction negative
const negativeNumerator = isProperFraction(-1, 4);
assertEquals(negativeNumerator, false);

// Example: 1/100 is a positive fraction
// Small numerator with large denominator
const smallFraction = isProperFraction(1, 100);
assertEquals(smallFraction, true);
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,31 @@

function getCardValue(card) {
// TODO: Implement this function
// Checks that input is a string and has correct length,
// 2 chars for 2-9/A/J/Q/K, 3 chars for 10
if (typeof card !== 'string' || (card.length !== 2 && card.length !==3)) {
throw new Error('Invalid card format');
}
// Get the suit, always the last character
const suit = card.slice(-1);
// Get the rank, everything except the last character
const rank = card.slice(0, -1);
// Checks if suit is one of the four allowed symbols
if (!'♠♥♦♣'.includes(suit)) {
throw new Error('Invalid suit');
}
// Special case: Ace is worth 11
if (rank === 'A') return 11;
// Special case: Face cards (Jack, Queen, King) are worth 10
if (rank === 'J' || rank === 'Q' || rank === 'K') return 10;
// For number cards (2-10): converts rank string into number
const value = Number(rank);
// Makes sure it's actually a valid number between 2 and 10
if (isNaN(value) || value < 2 || value > 10) {
throw new Error('Invalid rank');
}
// Return the numeric value for 2-10
return value;
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -41,7 +66,29 @@ function assertEquals(actualOutput, targetOutput) {
// Examples:
assertEquals(getCardValue("9♠"), 9);

// Valid number cards (2-10)
const twoofHearts = getCardValue("2♥");
assertEquals(twoofHearts, 2);

const fiveofDiamonds = getCardValue("5♦");
assertEquals(fiveofDiamonds, 5);

// Ace returns 11
const aceofSpades = getCardValue("A♠");
assertEquals(aceofSpades, 11);

// Face cards return 10 (J, Q, K)
const jackofHearts = getCardValue("J♥");
assertEquals(jackofHearts, 10);

const queenofDiamonds = getCardValue("Q♦");
assertEquals(queenofDiamonds, 10);

const kingofClubs = getCardValue("K♣");
assertEquals(kingofClubs, 10);

// Handling invalid cards
// Invalid format, random string
try {
getCardValue("invalid");

Expand All @@ -50,3 +97,32 @@ try {
} catch (e) {}

// What other invalid card cases can you think of?
// Too short, missing suit
try {
getCardValue("A");
console.error("Error was not thrown for too short card");
} catch (e) {}

// Invalid rank
try {
getCardValue("X♠");
console.error("Error was not thrown for invalid rank");
} catch (e) {}

// Invalid suit, wrong symbol
try {
getCardValue("Kx");
console.error("Error was not thrown for missing suit");
} catch (e) {}

// Missing suit
try {
getCardValue("10");
console.error("Error was not thrown for missing suit");
} catch (e) {}

// Empty string
try {
getCardValue("");
console.error("Error was not thrown for empty string");
} catch (e) {}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,33 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

// Case 2: Right angle
test(`should return "Right angle" when angle is exactly 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(135)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
});

// Case 4: Straight angle
test(`should return "Straight angle" when angle is exactly 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(270)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test(`should return "Invalid angle" when angle (<= 0 or >= 360)`, () => {
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(-30)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(400)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,47 @@ 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
// Special case: denominator is zero
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});

// Special case: numerator is zero
test(`should return true when numerator is zero`, () => {
expect(isProperFraction(0, 4)).toEqual(true);
});

// Special case: numerator equals denominator
test(`should return false when numerator equals denominator`, () => {
expect(isProperFraction(7, 7)).toEqual(false);
});

// Special case: numerator is smaller than denominator
test(`should return true when numerator is smaller than denominator`, () => {
expect(isProperFraction(1, 2)).toEqual(true);
});

// Special case: positive proper fraction
test(`should return true for positive proper fraction`, () => {
expect(isProperFraction(3, 5)).toEqual(true);
});

// Special case: numerator is larger than denominator
test(`should return false when numerator is larger than denominator`, () => {
expect(isProperFraction(8, 3)).toEqual(false);
});

// Special case: negative denominator
test(`should return false when denominator is negative`, () => {
expect(isProperFraction(2, -6)).toEqual(false);
});

// Special case: negative numerator
test(`should return false when numerator is negative`, () => {
expect(isProperFraction(-1, 4)).toEqual(false);
});

// Special case: small positive proper fraction
test(`should return true for small positive proper fraction`, () => {
expect(isProperFraction(1, 100)).toEqual(true);
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,35 @@ test(`Should return 11 when given an ace card`, () => {
// Face Cards (J, Q, K)
// Invalid Cards

// Case 2: Number Cards (2-10)
test(`Should return correct value for single digit number card`, () => {
expect(getCardValue("9♠")).toEqual(9);
expect(getCardValue("5♦")).toEqual(5);
expect(getCardValue("10♣")).toEqual(10);
});

// Case 3: Face cards (J, Q, K)
test(`Should return 10 for face cards (J, Q, K)`, () => {
expect(getCardValue("J♥")).toEqual(10);
expect(getCardValue("Q♦")).toEqual(10);
expect(getCardValue("K♣")).toEqual(10);
});

// Case 4: Invalid Cards
test(`Should throw error for invalid cards`, () => {
expect(() => getCardValue("invalid")).toThrow();
// too short
expect(() => getCardValue("A")).toThrow();
// invalid rank
expect(() => getCardValue("X♠")).toThrow();
// invalid suit
expect(() => getCardValue("Kx")).toThrow();
// missing suit
expect(() => getCardValue("10")).toThrow();
// empty string
expect(() => getCardValue("")).toThrow();
});

// 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