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 @@ -8,30 +8,63 @@
// - "Reflex angle" for angles greater than 180° and less than 360°
// - "Invalid angle" for angles outside the valid range.

// 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.
// Assumption: The parameter is a valid number.

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
// Helper function for testing
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === 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");
// ======================
// Tests
// ======================

// Acute angles
assertEquals(getAngleType(45), "Acute angle");
assertEquals(getAngleType(1), "Acute angle");
assertEquals(getAngleType(89), "Acute angle");

// Right angle
assertEquals(getAngleType(90), "Right angle");

// Obtuse angles
assertEquals(getAngleType(120), "Obtuse angle");
assertEquals(getAngleType(179), "Obtuse angle");
assertEquals(getAngleType(91), "Obtuse angle");

// Straight angle
assertEquals(getAngleType(180), "Straight angle");

// Reflex angles
assertEquals(getAngleType(200), "Reflex angle");
assertEquals(getAngleType(359), "Reflex angle");
assertEquals(getAngleType(181), "Reflex angle");

// Invalid angles
assertEquals(getAngleType(0), "Invalid angle");
assertEquals(getAngleType(360), "Invalid angle");
assertEquals(getAngleType(-10), "Invalid angle");
assertEquals(getAngleType(400), "Invalid angle");

console.log("All tests passed!");
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,56 @@

// 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.
// A proper fraction is when |numerator| < |denominator|

function isProperFraction(numerator, denominator) {
// TODO: Implement this function

// A fraction cannot have denominator 0
if (denominator === 0) {
return false;
}

// Check if absolute value of numerator is less than denominator
if (Math.abs(numerator) < Math.abs(denominator)) {
return true;
} 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;

// Here's our helper again
// Helper function for testing
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}

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

// Example: 1/2 is a proper fraction
// Tests
// ========================

// Proper fractions
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(3, 4), true);
assertEquals(isProperFraction(-1, 2), true);
assertEquals(isProperFraction(2, -5), true);
assertEquals(isProperFraction(-3, -7), true);

// Not proper (equal values)
assertEquals(isProperFraction(5, 5), false);
assertEquals(isProperFraction(-4, -4), false);

// Not proper (numerator larger)
assertEquals(isProperFraction(7, 3), false);
assertEquals(isProperFraction(-9, 4), false);
assertEquals(isProperFraction(10, -2), false);

// Denominator zero (invalid fraction)
assertEquals(isProperFraction(1, 0), false);
assertEquals(isProperFraction(0, 0), false);

console.log("All tests passed!");
Original file line number Diff line number Diff line change
@@ -1,52 +1,101 @@
// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck

//
// ORIGINAL QUESTION:
// 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♦".

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

//
// When the card string is invalid (not following the above format), the function should
// throw an error.

//
// 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
// 1. Separate the rank from the suit emoji
const rank = card.slice(0, -1);
const suit = card.slice(-1);

// 2. Validate the suit
const validSuits = ["♠", "♥", "♦", "♣"];
if (!validSuits.includes(suit)) {
throw new Error("Invalid suit");
}

// 3. Calculate value based on the rank
if (rank === "A") {
return 11;
}

if (rank === "J" || rank === "Q" || rank === "K") {
return 10;
}

// 4. Handle number cards (2-10)
const numericRank = parseInt(rank, 10);

// numericRank.toString() === rank makes sure rank was really a clean number string
// Example: parseInt("9", 10) -> 9, "9" === "9" ✅
// Example: parseInt("9x", 10) -> 9, "9" === "9x" ❌ (so invalid)
if (numericRank >= 2 && numericRank <= 10 && numericRank.toString() === rank) {
return numericRank;
}

// 5. If it reaches here, the rank was invalid
throw new Error("Invalid 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.
// Export for other test files (Jest step later)
module.exports = getCardValue;

// Helper functions to make our assertions easier to read.
// Helper function for testing (console.assert style)
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:
// =======================
// TESTS
// =======================

// Test Aces and Face Cards
assertEquals(getCardValue("A♠"), 11);
assertEquals(getCardValue("J♥"), 10);
assertEquals(getCardValue("Q♦"), 10);
assertEquals(getCardValue("K♣"), 10);

// Test Numbers
assertEquals(getCardValue("9♠"), 9);
assertEquals(getCardValue("10♥"), 10);
assertEquals(getCardValue("2♣"), 2);

// Handling invalid cards
// Test Invalid Inputs (should throw errors)
try {
getCardValue("invalid");
console.error("Error was not thrown for 'invalid'");
} catch (e) {}

// This line will not be reached if an error is thrown as expected
console.error("Error was not thrown for invalid card");
try {
getCardValue("1♠"); // There is no 1 card, only A,2..10,J,Q,K
console.error("Error was not thrown for '1♠'");
} catch (e) {}

try {
getCardValue("A?"); // invalid suit
console.error("Error was not thrown for 'A?'");
} catch (e) {}

// What other invalid card cases can you think of?
console.log("Tests complete!");
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 === 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(200)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test(`should return "Invalid angle" when angle is outside the valid range`, () => {
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(-1)).toEqual("Invalid angle");
expect(getAngleType(361)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,45 @@
// 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.

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


// Case 2: Numerator is zero
test(`should return true when numerator is zero and denominator is not zero`, () => {
expect(isProperFraction(0, 5)).toEqual(true);
expect(isProperFraction(0, -10)).toEqual(true);
});


// Case 3: Positive proper fractions
test(`should return true for positive proper fractions`, () => {
expect(isProperFraction(1, 2)).toEqual(true);
expect(isProperFraction(3, 4)).toEqual(true);
});


// Case 4: Positive improper fractions
test(`should return false for positive improper fractions`, () => {
expect(isProperFraction(5, 2)).toEqual(false);
expect(isProperFraction(4, 4)).toEqual(false);
});


// Case 5: Negative proper fractions
test(`should return true for negative proper fractions`, () => {
expect(isProperFraction(-1, 2)).toEqual(true);
expect(isProperFraction(2, -5)).toEqual(true);
expect(isProperFraction(-3, -7)).toEqual(true);
});


// Case 6: Negative improper fractions
test(`should return false for negative improper fractions`, () => {
expect(isProperFraction(-5, 2)).toEqual(false);
expect(isProperFraction(9, -4)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,44 @@
// We will use the same function, but write tests for it using Jest in this file.
const getCardValue = require("../implement/3-get-card-value");

// TODO: Write tests in Jest syntax to cover all possible outcomes.

// Case 1: Ace (A)
// Case 1: Ace
test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
expect(getCardValue("A♥")).toEqual(11);
});

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
// Face Cards (J, Q, K)
// Invalid Cards

// 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
// Case 2: Number Cards (2-10)
test(`Should return correct number for number cards`, () => {
expect(getCardValue("2♣")).toEqual(2);
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 - Invalid suit
test(`Should throw error for invalid suit`, () => {
expect(() => getCardValue("A?")).toThrow();
});


// Case 5: Invalid Cards - Invalid rank
test(`Should throw error for invalid rank`, () => {
expect(() => getCardValue("1♠")).toThrow();
expect(() => getCardValue("11♣")).toThrow();
});


// Case 6: Completely invalid string
test(`Should throw error for completely invalid input`, () => {
expect(() => getCardValue("invalid")).toThrow();
});