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 <= 0 || angle >= 360) {
return "Invalid angle";
} else if (angle > 0 && angle < 90) {
return "Acute angle";
} else if (angle > 90 && angle < 180) {
return "Obtuse angle";
} else if (angle === 90) {
return "Right angle";
} else if (angle === 180) {
return "Straight angle";
} else if (angle > 180 && angle < 360) {
return "Reflex angle";
}
}

// The line below allows us to load the getAngleType function into tests in other files.
Expand All @@ -34,4 +46,22 @@ function assertEquals(actualOutput, targetOutput) {
// TODO: Write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles
const right = getAngleType(90);
const acute1 = getAngleType(20);
const invalid = getAngleType(0);
const acute3 = getAngleType(89.999);
const obtuse = getAngleType(98.25);
const straightAngle = getAngleType(180);
const invalid1 = getAngleType(360);
const invalidAngle = getAngleType(362);
const reflexAngle2 = getAngleType(210);

assertEquals(right, "Right angle");
assertEquals(acute1, "Acute angle");
assertEquals(invalid, "Invalid angle");
assertEquals(obtuse, "Obtuse angle");
assertEquals(straightAngle, "Straight angle");
assertEquals(invalid1, "Invalid angle");
assertEquals(invalidAngle, "Invalid angle");
assertEquals(reflexAngle2, "Reflex angle");
assertEquals(acute3, "Acute angle");

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if(numerator>denominator) {
return false
}else{
return true
}
}

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

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(1, 10000), true);
assertEquals(isProperFraction(100, 2), false);
assertEquals(isProperFraction(1, 0.005), false);
assertEquals(isProperFraction(1e2, 1e3), true);
assertEquals(isProperFraction(1e4, 1e0), false);
assertEquals(isProperFraction(-1, 1), true);
assertEquals(isProperFraction(1, -1), false);
assertEquals(isProperFraction(0, -1), false);
assertEquals(isProperFraction(-1, 0), true);
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// "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♦".
// For example: "A♠", "2♥", "10♥", "J"2♠"", "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.
Expand All @@ -22,7 +22,33 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
if (
!card.includes("♠") &&
!card.includes("♥") &&
!card.includes("♦") &&
!card.includes("♣")
){
throw new Error("Invalid String");
}
const rank = card.slice(0, -1);

if (
!card.includes("A") &&
!card.includes("J") &&
!card.includes("Q") &&
!card.includes("K") &&
!(rank >= 2 && rank <= 10)
) {
throw new Error("Invalid String");
}

if (rank==="A") {
return 11;
} else if (rank==="J" || rank==="Q" || rank==="K") {
return 10;
} else if (rank >= 2 && rank <= 10) {
return rank;
}
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -40,6 +66,18 @@ function assertEquals(actualOutput, targetOutput) {
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:
assertEquals(getCardValue("9♠"), 9);
assertEquals(getCardValue("A♠"), 11);
assertEquals(getCardValue("J♠"), 10);
assertEquals(getCardValue("K♠"), 10);
assertEquals(getCardValue("Q♠"), 10);
assertEquals(getCardValue("2♠"), 2);
assertEquals(getCardValue("10♠"), 10);
assertEquals(getCardValue(" ♠"), "Invalid String");
assertEquals(getCardValue(" 6 ♠"), "Invalid String");
assertEquals(getCardValue(" ♠"), "Invalid String");
assertEquals(getCardValue(" **♠"), "Invalid String");
assertEquals(getCardValue("10 **"), "Invalid String");
assertEquals(getCardValue("A10♠"), "Invalid String");

// Handling invalid cards
try {
Expand All @@ -50,3 +88,5 @@ try {
} catch (e) {}

// What other invalid card cases can you think of?
// card with a mix of suit
//card with 0 at the front 01♠
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,40 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

// Case 2: Right angle
test(`should return "Right Angles" when(angle===90)`, () => {
expect(getAngleType(90)).toEqual("Right angle");
});

// Case 3: Obtuse angles

test(`should return "Obtuse angles" when (90< angle <180)`, () => {
expect(getAngleType(91)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
expect(getAngleType(160)).toEqual("Obtuse angle");
expect(getAngleType(100)).toEqual("Obtuse angle");
expect(getAngleType(151.55)).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(270)).toEqual("Reflex angle");
expect(getAngleType(350)).toEqual("Reflex angle");
expect(getAngleType(359.5)).toEqual("Reflex angle");
expect(getAngleType(210)).toEqual("Reflex angle");
expect(getAngleType(200)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test(`Should return "Invalid angle" when (0 =<angle>=360)`, () => {
expect(getAngleType(0).toEqual("Invalid angle"));
expect(getAngleType(-1).toEqual("Invalid angle"));
expect(getAngleType(360).toEqual("Invalid angle"));
expect(getAngleType(500).toEqual("Invalid angle"));
expect(getAngleType("1e5").toEqual("Invalid angle"));
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ 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(1, -1)).toEqual(false);
expect(isProperFraction(10, 1)).toEqual(false);
expect(isProperFraction(1, 10)).toEqual(true);
expect(isProperFraction(-1, 0)).toEqual(true);
expect(isProperFraction(1e2, 1e4)).toEqual(true);
expect(isProperFraction(1e4, 1e0)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,44 @@ 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 Number from "2-10" when given a number card`,()=>{

expect(getCardValue("2♠")).toEqual(2);
expect(getCardValue("3♠")).toEqual(3);
expect(getCardValue("4♣")).toEqual(4);
expect(getCardValue("5♠")).toEqual(5);
expect(getCardValue("6♠")).toEqual(6);
expect(getCardValue("7♣")).toEqual(7);
expect(getCardValue("8♠")).toEqual(8);
expect(getCardValue("9♠")).toEqual(9);
expect(getCardValue("10♠")).toEqual(10);
})


// Face Cards (J, Q, K)

test(`Should return 10 when given a face card`,()=>{

expect(getCardValue("J♣")).toEqual(10);
expect(getCardValue("K♣")).toEqual(10);
expect(getCardValue("Q♣")).toEqual(10);
expect(getCardValue("Q♠")).toEqual(10);
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("K♠")).toEqual(10);
})
// Invalid Cards

test('throws on Invalid String', () => {
expect(() => { getCardValue('A10♠');}).toThrow("Invalid String");
expect(() => { getCardValue('A');}).toThrow("Invalid String");
expect(() => { getCardValue('110♠');}).toThrow("Invalid String");
expect(() => { getCardValue(' ♠');}).toThrow("Invalid String");
expect(() => { getCardValue('eeeeeej1234');}).toThrow("Invalid String");

});



// 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
Expand Down
Loading