Skip to content
Closed
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 @@ -10,28 +10,30 @@

// Assumption: The parameter is a valid number. (You do not need to handle non-numeric inputs.)

// 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 getAngleType(angle) {
// 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.
// This will be useful in the "rewrite tests with jest" step.
module.exports = getAngleType;

// This helper function is written to make our assertions easier to read.
// If the actual output matches the target output, the test will pass
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
`Expected "${actualOutput}" to equal "${targetOutput}"`
);
}

// TODO: Write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@

// Assumption: The parameters are valid numbers (not NaN or Infinity).

// Note: If you are unfamiliar with proper fractions, please look up its mathematical definition.

// 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 isProperFraction(numerator, denominator) {
// TODO: Implement this function
// A denominator of 0 is mathematically undefined, so it cannot be a proper fraction.
if (denominator === 0) {
return false;
}

// A fraction is proper if the absolute value of the numerator
// is strictly less than the absolute value of the denominator.
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;

// Here's our helper again
Expand All @@ -26,8 +26,3 @@ function assertEquals(actualOutput, targetOutput) {
);
}

// TODO: Write tests to cover all cases.
// What combinations of numerators and denominators should you test?

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
108 changes: 75 additions & 33 deletions Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,96 @@
// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck

// Implement a function getCardValue, when given a string representing a playing card,
// should return the numerical value of the card.

// A valid card string will contain a rank followed by the suit.
// The rank can be one of the following strings:
// "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"
// The suit can be one of the following emojis:
// "♠", "♥", "♦", "♣"
// For example: "A♠", "2♥", "10♥", "J♣", "Q♦", "K♦".
function getCardValue(card) {
// Validate that input is a non-empty string and has at least 2 characters (Rank + Suit)
if (typeof card !== "string" || card.length < 2) {
throw new Error("Invalid card format");
}

// Define valid suits
const validSuits = ["♠", "♥", "♦", "♣"];

// When the card is an ace ("A"), the function should return 11.
// When the card is a face card ("J", "Q", "K"), the function should return 10.
// When the card is a number card ("2" to "10"), the function should return its numeric value.
// The suit is always the last character/emoji of the string.
// Using Array.from() or string methods safely extracts it.
const suit = card.slice(-1);
if (!validSuits.includes(suit)) {
throw new Error("Invalid card suit");
}

// When the card string is invalid (not following the above format), the function should
// throw an error.
// The rank is everything up to the suit emoji.
const rank = card.slice(0, -1);

// Acceptance criteria:
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.
// Handle value mappings
if (rank === "A") {
return 11;
}

if (["J", "Q", "K"].includes(rank)) {
return 10;
}

function getCardValue(card) {
// TODO: Implement this function
// Parse numeric ranks ("2" through "10")
const numericValue = parseInt(rank, 10);
if (!isNaN(numericValue) && numericValue >= 2 && numericValue <= 10 && String(numericValue) === rank) {
return numericValue;
}

// If the rank doesn't match any criteria, it's invalid.
throw new Error("Invalid card rank");
}

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

// Helper functions to make our assertions easier to read.
// ==========================================
// ASSERTION HELPERS
// ==========================================

function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}

// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:
assertEquals(getCardValue("9♠"), 9);
// A helper to verify that an invalid input throws an error as expected
function assertThrows(invalidCard) {
try {
getCardValue(invalidCard);
console.error(`❌ Error was NOT thrown for invalid card: "${invalidCard}"`);
} catch (e) {
// Test passes if an error is thrown
}
}

// Handling invalid cards
try {
getCardValue("invalid");
// ==========================================
// TEST SUITE
// ==========================================

// 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 🎉");
}
console.log("Running tests...");

// 1. Valid Aces (Value: 11)
assertEquals(getCardValue("A♠"), 11);
assertEquals(getCardValue("A♥"), 11);

// 2. Valid Face Cards (Value: 10)
assertEquals(getCardValue("J♣"), 10);
assertEquals(getCardValue("Q♦"), 10);
assertEquals(getCardValue("K♠"), 10);

// 3. Valid Numeric Boundary Cards
assertEquals(getCardValue("2♥"), 2);
assertEquals(getCardValue("5♦"), 5);
assertEquals(getCardValue("9♠"), 9);
assertEquals(getCardValue("10♣"), 10);

// 4. Invalid Card Scenarios (Should throw errors)
assertThrows("invalid"); // Completely wrong format
assertThrows("A"); // Missing suit
assertThrows("♠"); // Missing rank
assertThrows("1♠"); // 1 is not a valid rank (should be "A")
assertThrows("11♥"); // Out-of-bounds number card
assertThrows("A♣️"); // Suit variations or hidden characters
assertThrows("Q⭐️"); // Invalid suit emoji
assertThrows("J ♠"); // Unwanted spacing

// What other invalid card cases can you think of?
console.log("All tests completed!");
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,52 @@
// We will use the same function, but write tests for it using Jest in this file.
const getAngleType = require("../implement/1-get-angle-type");

// TODO: Write tests in Jest syntax to cover all cases/outcomes,
// including boundary and invalid cases.
describe("getAngleType", () => {

// Case 1: Acute angles
test('should return "Acute angle" when (0 < angle < 90)', () => {
expect(getAngleType(1)).toBe("Acute angle");
expect(getAngleType(45)).toBe("Acute angle");
expect(getAngleType(89.9)).toBe("Acute angle");
});

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

// Case 2: Right angle
// Case 3: Obtuse angles
// Case 4: Straight angle
// Case 5: Reflex angles
// Case 6: Invalid angles
// Case 3: Obtuse angles
test('should return "Obtuse angle" when (90 < angle < 180)', () => {
expect(getAngleType(90.1)).toBe("Obtuse angle");
expect(getAngleType(135)).toBe("Obtuse angle");
expect(getAngleType(179.9)).toBe("Obtuse angle");
});

// Case 4: Straight angle
test('should return "Straight angle" when angle is exactly 180', () => {
expect(getAngleType(180)).toBe("Straight angle");
});

// Case 5: Reflex angles
test('should return "Reflex angle" when (180 < angle < 360)', () => {
expect(getAngleType(180.1)).toBe("Reflex angle");
expect(getAngleType(270)).toBe("Reflex angle");
expect(getAngleType(359.9)).toBe("Reflex angle");
});

// Case 6: Invalid angles
test('should return "Invalid angle" for angles outside the 0 to 360 range', () => {
// Negative angles
expect(getAngleType(-45)).toBe("Invalid angle");

// Lower boundary
expect(getAngleType(0)).toBe("Invalid angle");

// Upper boundary
expect(getAngleType(360)).toBe("Invalid angle");

// Exceeding upper boundary
expect(getAngleType(361)).toBe("Invalid angle");
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,50 @@
// We will use the same function, but write tests for it using Jest in this file.
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.
describe("isProperFraction", () => {

// 1. Positive Proper Fractions
test("should return true for positive proper fractions (numerator < denominator)", () => {
expect(isProperFraction(1, 2)).toBe(true);
expect(isProperFraction(3, 4)).toBe(true);
expect(isProperFraction(99, 100)).toBe(true);
});

// 2. Positive Improper Fractions
test("should return false for positive improper fractions (numerator >= denominator)", () => {
expect(isProperFraction(5, 4)).toBe(false);
expect(isProperFraction(10, 2)).toBe(false);
expect(isProperFraction(4, 4)).toBe(false); // Exactly 1
});

// 3. Zero Cases
test("should return false when denominator is zero", () => {
expect(isProperFraction(1, 0)).toBe(false);
expect(isProperFraction(0, 0)).toBe(false);
});

test("should return true when numerator is zero and denominator is non-zero", () => {
expect(isProperFraction(0, 5)).toBe(true);
expect(isProperFraction(0, -5)).toBe(true);
});

// 4. Negative Fractions
test("should evaluate proper fractions correctly when negative signs are present", () => {
expect(isProperFraction(-1, 3)).toBe(true); // Negative numerator
expect(isProperFraction(1, -3)).toBe(true); // Negative denominator
expect(isProperFraction(-1, -3)).toBe(true); // Both negative
});

test("should evaluate improper fractions correctly when negative signs are present", () => {
expect(isProperFraction(-5, 4)).toBe(false); // Magnitude > 1
expect(isProperFraction(5, -4)).toBe(false); // Magnitude > 1
expect(isProperFraction(-4, -4)).toBe(false); // Magnitude = 1
});

// 5. Decimals / Floating Point Numbers
test("should handle decimal inputs using absolute magnitude values", () => {
expect(isProperFraction(1.5, 3)).toBe(true);
expect(isProperFraction(4.5, 3)).toBe(false);
});

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