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

function getAngleType(angle) {
// TODO: Implement this function
if (angle === 90) {
return "Right angle";
}
if (angle < 90) {
return "Acute angle";
}
if (angle > 90 && angle < 180) {
return "Obtuse angle";
}
if (angle === 180) {
return "Straight angle";
}
return "Reflex angle";
}

// The line below allows us to load the getAngleType function into tests in other files.
Expand All @@ -35,3 +47,27 @@ 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 less than 90 degrees,
// Then the function should return "Acute angle"
const acute = getAngleType(45);
assertEquals(acute, "Acute angle");

// Case 3: Identify Obtuse Angles:
// When the angle is greater than 90 degrees and less than 180 degrees,
// Then the function should return "Obtuse angle"
const obtuse = getAngleType(120);
assertEquals(obtuse, "Obtuse angle");

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

// Case 5: Identify Reflex Angles:
// When the angle is greater than 180 degrees and less than 360 degrees,
// Then the function should return "Reflex angle"
const reflex = getAngleType(340);
assertEquals(reflex, "Reflex angle");
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (!numerator || !denominator) return "Incomplete fraction";
if (typeof numerator !== "number" || typeof denominator !== "number")
return "Use only numbers";

return Math.abs(numerator) < Math.abs(denominator);
}

// The line below allows us to load the isProperFraction function into tests in other files.
Expand All @@ -29,5 +33,70 @@ 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);
// Proper Fraction check:
// Input: numerator = 2, denominator = 3
// target output: true
// Explanation: The fraction 2/3 is a proper fraction, where the numerator is less than the denominator. The function should return true.
const properFraction = isProperFraction(2, 3);
assertEquals(properFraction, true);

// Improper Fraction check:
// Input: numerator = 5, denominator = 2
// target output: false
// Explanation: The fraction 5/2 is an improper fraction, where the numerator is greater than or equal to the denominator. The function should return false.
const improperFraction = isProperFraction(5, 2);
assertEquals(improperFraction, false);

// Negative Fraction check:
// Input: numerator = -4, denominator = 7
// target output: true
// Explanation: The fraction -4/7 is a proper fraction because the absolute value of the numerator (4) is less than the denominator (7). The function should return true.
const negativeFraction = isProperFraction(-4, 7);
assertEquals(negativeFraction, true);

// Equal Numerator and Denominator check:
// Input: numerator = 3, denominator = 3
// target output: false
// Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false.
const equalFraction = isProperFraction(3, 3);
assertEquals(equalFraction, false);
// ====> complete with your assertion

// Stretch:
// What other scenarios could you test for?

// Negative Fraction check:
// Input: numerator = 4, denominator = -7
// target output: true
// Explanation: The fraction 4/-7 is a proper fraction because the numerator (4) is less than the absolute value of the denominator (7). The function should return true.
const negativeFraction2 = isProperFraction(4, -7);
assertEquals(negativeFraction2, true);

// Negative Fraction check:
// Input: numerator = -4, denominator = -7
// target output: true
// Explanation: The fraction -4/-7 is a proper fraction because the absolute value of the numerator (4) is less than the absolute value of the denominator (7). The function should return true.
const negativeFraction3 = isProperFraction(-4, -7);
assertEquals(negativeFraction3, true);

//Invalid input
//Input:numerator = "q", denominator = 7
// target output: message
//Explanation: the fraction only takes numbers
const notANumber = isProperFraction("q", 7);
assertEquals(notANumber, "Use only numbers");

//Invalid input
//Input:numerator = 7, denominator = "q"
// target output: message
//Explanation: the fraction only takes numbers
const notANumber2 = isProperFraction(7, "q");
assertEquals(notANumber2, "Use only numbers");


//Incomplete input
//Input:2
// target output: message
//Explanation: the fraction needs both a numerator and a denominator
const incompleteFraction = isProperFraction(2);
assertEquals(incompleteFraction, "Incomplete fraction");
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,24 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
let rank = card.slice(0, -1);
let cardFace = card[card.length - 1];

if (!["♠", "♥", "♦", "♣"].includes(cardFace)) {
throw new Error(`Invalid card face: ${cardFace}`);
}
if (rank === "A") {
return 11;
}
if (+rank >= 2 && +rank <= 9) {
return +rank;
}
if (["K", "10", "Q", "J"].includes(rank)) {
return 10;
}
if (!["♠", "♥", "♦", "♣"].includes(cardFace)) {
}
throw new Error(`Invalid card rank: ${rank}`);
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -37,16 +54,46 @@ function assertEquals(actualOutput, targetOutput) {
);
}

// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:
assertEquals(getCardValue("9♠"), 9);
// Acceptance criteria:

// Handling invalid cards
try {
getCardValue("invalid");
// Given a card string in the format "A♠" (representing a card in blackjack - the last character will always be an emoji for a suit, and all characters before will be a number 2-10, or one letter of J, Q, K, A),
// When the function getCardValue is called with this card string as input,
// Then it should return the numerical card value
const aceOfSpades = getCardValue("A♠");
assertEquals(aceOfSpades, 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) {}
// Handle Number Cards (2-10):
// Given a card with a rank between "2" and "9",
// When the function is called with such a card,
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
const fiveOfHearts = getCardValue("5♥");
assertEquals(fiveOfHearts, 5);

// What other invalid card cases can you think of?
// Handle Face Cards (J, Q, K):
// Given a card with a rank of "10," "J," "Q," or "K",
// When the function is called with such a card,
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
const kingOfHearts = getCardValue("K♥");
assertEquals(kingOfHearts, 10);
// Handle Ace (A):
// Given a card with a rank of "A",
// When the function is called with an Ace,
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.

// Handle Invalid Cards:
// Given a card with an invalid rank (neither a number nor a recognized face card),
// When the function is called with such a card,
// Then it should throw an error indicating "Invalid card rank."
try {
getCardValue("2");
throw new Error("Should have thrown an error for invalid card");
} catch (error) {
assertEquals(error.message, "Invalid card face: 2");
}

try {
getCardValue("1♥");
throw new Error("Should have thrown an error for invalid card");
} catch (error) {
assertEquals(error.message, "Invalid card rank: 1");
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,35 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
expect(getAngleType(89)).toEqual("Acute angle");
});

// Case 2: Right angle
// Case 3: Obtuse angles
// Case 4: Straight angle
// Case 5: Reflex angles
// Case 6: Invalid angles
// REPLACE the comments with the tests
// make your test descriptions as clear and readable as possible

// Case 2: Identify Acute Angles:
// When the angle is less than 90 degrees,
// Then the function should return "Acute angle"
test("should identify acute angle (80°)",()=>{
expect(getAngleType(80)).toEqual("Acute angle");
});

// Case 3: Identify Obtuse Angles:
// When the angle is greater than 90 degrees and less than 180 degrees,
// Then the function should return "Obtuse angle"
test("should identify obtuse angle(110°)",()=>{
expect(getAngleType(110)).toEqual("Obtuse angle")
});


// Case 4: Identify Straight Angles:
// When the angle is exactly 180 degrees,
// Then the function should return "Straight angle"
test("should identify straight angle(180°)",()=>{
expect(getAngleType(180)).toEqual("Straight angle")
});


// Case 5: Identify Reflex Angles:
// When the angle is greater than 180 degrees and less than 360 degrees,
// Then the function should return "Reflex angle"
test("should identify reflex angle(280°)",()=>{
expect(getAngleType(280)).toEqual("Reflex angle")
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,18 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});

// Case 2: Identify Improper Fractions:
test("should return false for improper fraction",()=>{
expect(isProperFraction(3,2)).toEqual(false)
})

// Case 3: Identify Negative Fractions:
test("should return true for negative proper fraction",()=>{
expect(isProperFraction(-2,7)).toEqual(true)
})

// Case 4: Identify Equal Numerator and Denominator:
test("should return false for equal nominator and denominator",()=>{
expect(isProperFraction(2,2)).toEqual(false)
})
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@
// 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)
test(`Should return 11 when given an ace card`, () => {
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
// Case 2: Handle Number Cards (2-10):
test("should return 5 for five of spades ",()=>{
const fiveOfSpades = getCardValue("5♠");
expect(fiveOfSpades).toEqual(5)
})
// Case 3: Handle Face Cards (J, Q, K):
test("should return 10 for king of spades",()=>{
const kingOfSpades = getCardValue("K♠");
expect(kingOfSpades).toEqual(10)
})
// Case 4: Handle Ace (A):
test("should return 11 for Ace of Spades", () => {
const aceOfSpades = getCardValue("A♠");
expect(aceOfSpades).toEqual(11);
});
// Case 5: Handle Invalid Cards:
test("should through error for 1 of spades",()=>{
expect(()=> getCardValue("1♠")).toThrow('Invalid card rank: 1')
})

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

test("should throw error for five*",()=>{
expect(() => getCardValue("5*")).toThrow("Invalid card face: *");
})