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

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.
Expand All @@ -35,3 +48,24 @@ function assertEquals(actualOutput, targetOutput) {
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");

const Acute = getAngleType(45);
assertEquals(Acute, "Acute angle")

const Obtuse = getAngleType(120);
assertEquals(Obtuse, "Obtuse angle")

const Straight = getAngleType(180);
assertEquals(Straight, "Straight angle")

const Reflex = getAngleType(270);
assertEquals(Reflex, "Reflex angle")

const InvalidNegative= getAngleType(-10);
assertEquals(InvalidNegative, "Invalid angle")

const InvalidOver= getAngleType(400);
assertEquals(InvalidOver, "Invalid angle")

const InvalidZero= getAngleType(0);
assertEquals(InvalidZero, "Invalid angle")
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {
if (denominator >numerator && denominator !==0) {
return ture ;

}
else{
return false;
}
return Math.abs(numerator) < Math.abs(denominator);
// TODO: Implement this function
}

Expand All @@ -31,3 +39,16 @@ function assertEquals(actualOutput, targetOutput) {

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

//2/1 is not a proper fraction
assertEquals(isProperFraction(2,1), false);
//0/1 is a proper fraction
assertEquals(isProperFraction(0,1), true);
//negative fraction -1/2 is a proper fraction
assertEquals(isProperFraction(-1,2), true);
//negative fraction -1/-2 is not a proper fraction
assertEquals(isProperFraction(-1,-2), false);
//fraction with zero denominator is not a proper fraction
assertEquals(isProperFraction(1,0), false);
//fraction with zero numerator is a proper fraction
assertEquals(isProperFraction(0,5), true);
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,27 @@

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.
// This will be useful in the "rewrite tests with jest" step.
module.exports = getCardValue;
Expand All @@ -39,14 +58,61 @@ function assertEquals(actualOutput, targetOutput) {

// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:
assertEquals(getCardValue("9♠"), 9);
// (Ace All suits)
const aceOfSpades = getCardValue("A♠");
assertEquals(aceOfSpades, 11);

const aceOfHearts = getCardValue("A♥");
assertEquals(aceOfHearts, 11);

const aceOfDiamonds = getCardValue("A♦");
assertEquals(aceOfDiamonds, 11);

const aceOfClubs = getCardValue("A♣");
assertEquals(aceOfClubs, 11);

// (Face cards)
const jackOfHearts = getCardValue("J♥");
assertEquals(jackOfHearts, 10);

const queenOfClubs = getCardValue("Q♣");
assertEquals(queenOfClubs, 10);

const kingOfSpades = getCardValue("K♠");
assertEquals(kingOfSpades, 10);

// (Number cards)
const threeOfDiamonds = getCardValue("3♦");
assertEquals(threeOfDiamonds, 3);

const sevenOfClubs = getCardValue("7♣");
assertEquals(sevenOfClubs, 7);



// Handling invalid cards
// giving an invalid rank (a number or an recognized face card)
// When the function is called with such a card,
// Then it should throw an error indicating "Invalid card rank."
try {
getCardValue("invalid");

// This line will not be reached if an error is thrown as expected
console.error("Error was not thrown for invalid card");
} catch (e) {}
} catch (error) {}

// What other invalid card cases can you think of?

try {
getCardValue("1♠");
console.error("Error was not thrown for invalid card rank");
} catch (error) {
assertEquals(error.message, "Invalid card rank");
}

try {
getCardValue("5X");
console.error("Error was not thrown for invalid card suit");
} catch (error) {
assertEquals(error.message, "Invalid card suit");
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,36 @@ 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)`, () => {
// Test various obtuse angles, including boundary cases
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)`, () => {
// Test various reflex angles, including boundary cases
expect(getAngleType(181)).toEqual("Reflex angle");
expect(getAngleType(270)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test(`should throw an error when (angle < 0 or angle >= 360)`, () => {
expect(() => getAngleType(-1)).toThrow("Invalid angle");
expect(() => getAngleType(360)).toThrow("Invalid angle");
});
test(`should throw an error when (angle === 0)`, () => {
expect(() => getAngleType(0)).toThrow("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,28 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
// Special case: numerator is zero
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
expect(isProperFraction(-5, 0)).toEqual(false);
});

test(`should return true for positive proper fractions`, () => {
expect(isProperFraction(1, 2)).toEqual(true);
expect(isProperFraction(2, 5)).toEqual(true);
});

test(`should return false for positive improper fractions or equal values`, () => {
expect(isProperFraction(3, 2)).toEqual(false);
expect(isProperFraction(5, 5)).toEqual(false);
});

test(`should return true when numerator is zero`, () => {
expect(isProperFraction(0, 5)).toEqual(true);
expect(isProperFraction(0, -5)).toEqual(true);
});

test(`should evaluate correctly with various negative number combinations`, () => {
expect(isProperFraction(-1, 2)).toEqual(true);
expect(isProperFraction(1, -2)).toEqual(true);
expect(isProperFraction(-1, -2)).toEqual(true);
expect(isProperFraction(-5, 2)).toEqual(false);
expect(isProperFraction(5, -2)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,27 @@ test(`Should return 11 when given an ace card`, () => {

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
test(`Should return the correct value for number cards (2-10)`), () => {
expect(getCardValue("2♠")).toEqual(2);
expect(getCardValue("5♠")).toEqual(5);
expect(getCardValue("10♠")).toEqual(10);
}
// Face Cards (J, Q, K)
// Invalid Cards
test(`Should return 10 when given a face card (J, Q, K)`), () => {
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("Q♠")).toEqual(10);
expect(getCardValue("K♠")).toEqual(10);
}
// Invalid Card suits
test(`Should throw an error when given an invalid card suit`), () => {
expect(() => getCardValue("3-")).toThrow("Invalid card rank");
expect(() => getCardValue("5X")).toThrow("Invalid card suit");

// Invalid Card ranks
test(`Should throw an error when given an invalid card rank`), () => {
expect(() => getCardValue("1♠")).toThrow("Invalid card rank");
expect(() => getCardValue("11♠")).toThrow("Invalid card rank");
}

// To learn how to test whether a function throws an error as expected in Jest,
// please refer to the Jest documentation:
Expand Down