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 (0 < angle && angle < 90) {
return "Acute angle";
} else if (angle === 90) {
return "Right angle";
} else if (90 < angle && angle < 180) {
return "Obtuse angle";
} else if (angle === 180) {
return "Straight angle";
} else if (180 < angle && 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 @@ -33,5 +45,15 @@ function assertEquals(actualOutput, targetOutput) {

// TODO: Write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles
const Acute = getAngleType(0.5);
assertEquals(Acute, "Acute angle");
const right = getAngleType(90);
assertEquals(right, "Right angle");
const Obtuse = getAngleType(179);
assertEquals(Obtuse, "Obtuse angle");
const Straight = getAngleType(180);
assertEquals(Straight, "Straight angle");
const Reflex = getAngleType(181);
assertEquals(Reflex, "Reflex angle");
const Invalid = getAngleType(361);
assertEquals(Invalid, "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (Math.abs(numerator) < Math.abs(denominator)) {
return true;
} else if (denominator === 0) {
return false;
} else {
return false;
}
}

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

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(0, 0.2), true);
assertEquals(isProperFraction(2, 0), false);
assertEquals(isProperFraction(11, 8), false);
assertEquals(isProperFraction(0, 1), true);
assertEquals(isProperFraction(10, 25), true);
assertEquals(isProperFraction(0, 0), false);
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,39 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
const validRank = [
"A",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"J",
"Q",
"K",
];
const validSuits = ["♠", "♣", "♦", "♥"];
let rank = card.slice(0, -1);
let suit = card.slice(-1);
if (!validSuits.includes(suit)) {
throw new Error("Invalid suit");
}
if (!validRank.includes(rank)) {
throw new Error("Invalid rank");
}
if (rank === "A") {
return 11;
}
if (["J", "Q", "K"].includes(rank)) {
return 10;
} else {
return Number(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 @@ -40,7 +70,13 @@ 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("7♣"), 7);
assertEquals(getCardValue("10♦"), 10);
assertEquals(getCardValue("2♣"), 2);
// Handling invalid cards
try {
getCardValue("invalid");
Expand All @@ -52,3 +88,51 @@ try {
}

// What other invalid card cases can you think of?
try {
getCardValue("1♥");
console.error("Error was not thrown for invalid card 😢");
} catch (e) {
console.log("Error thrown for invalid card 🎉");
}
try {
getCardValue(" ");
console.error("Error was not thrown for invalid card 😢");
} catch (e) {
console.log("Error thrown for invalid card 🎉");
}
try {
getCardValue("Z♠");
console.error("Error was not thrown for invalid card 😢");
} catch (e) {
console.log("Error thrown for invalid card 🎉");
}
try {
getCardValue("G♠");
console.error("Error was not thrown for invalid card 😢");
} catch (e) {
console.log("Error thrown for invalid card 🎉");
}
try {
getCardValue("10X");
console.error("Error was not thrown for invalid card 😢");
} catch (e) {
console.log("Error thrown for invalid card 🎉");
}
try {
getCardValue("B♠");
console.error("Error was not thrown for invalid card 😢");
} catch (e) {
console.log("Error thrown for invalid card 🎉");
}
try {
getCardValue("♠");
console.error("Error was not thrown for invalid card 😢");
} catch (e) {
console.log("Error thrown for invalid card 🎉");
}
try {
getCardValue("B♠♠");
console.error("Error was not thrown for invalid card 😢");
} catch (e) {
console.log("Error thrown for invalid card 🎉");
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,34 @@ 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 angles" when (90 < angle < 180)`, () => {
expect(getAngleType(91)).toEqual("Obtuse angles");
expect(getAngleType(179)).toEqual("Obtuse angles");
expect(getAngleType(145)).toEqual("Obtuse angles");
expect(getAngleType(92)).toEqual("Obtuse angles");
expect(getAngleType(178)).toEqual("Obtuse angles");
});
// 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 angles" when (180 < angle < 360)`, () => {
expect(getAngleType(181)).toEqual("Reflex angles");
expect(getAngleType(190)).toEqual("Reflex angles");
expect(getAngleType(345)).toEqual("Reflex angles");
expect(getAngleType(359)).toEqual("Reflex angles");
expect(getAngleType(277)).toEqual("Reflex angles");
});
// Case 6: Invalid angles
test(`should return "Invalid angles" when (angle )`, () => {
expect(getAngleType(-60)).toEqual("Invalid angles");
expect(getAngleType(450)).toEqual("Invalid angles");
expect(getAngleType(-2)).toEqual("Invalid angles");
expect(getAngleType(361)).toEqual("Invalid angles");
expect(getAngleType(890)).toEqual("Invalid angles");
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,14 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});
test(`should return false when the denominator less nominator`, () => {
expect(isProperFraction(7, 2)).toEqual(false);
expect(isProperFraction(-17, -2)).toEqual(false);
expect(isProperFraction(0.5, 0.1)).toEqual(false);
});
test(`should return true when denominator is greater than nominator`, () => {
expect(isProperFraction(2, 8)).toEqual(true);
expect(isProperFraction(-3, -10)).toEqual(true);
expect(isProperFraction(-3, 7)).toEqual(true);
expect(isProperFraction(8, 17)).toEqual(true);
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,30 @@ const getCardValue = require("../implement/3-get-card-value");
test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
});
//Case 2: Face Cards (J, Q, K)
test(`Should return 10 when given a Face Cards`, () => {
expect(getCardValue("J♣")).toEqual(10);
expect(getCardValue("K♦")).toEqual(10);
expect(getCardValue("Q♥")).toEqual(10);
});
//Case 3: Number Cards (2-10)
test(`Should return the Number it self when a given Number card`, () => {
expect(getCardValue("2♣")).toEqual(2);
expect(getCardValue("10♦")).toEqual(10);
expect(getCardValue("4♥")).toEqual(4);
expect(getCardValue("6♣")).toEqual(6);
expect(getCardValue("7♠")).toEqual(7);
expect(getCardValue("9♥")).toEqual(9);
});
// Invalid Cards
test(`Should return Invalid Cards when given Invalid Cards`, () => {
expect(getCardValue("22♣")).toEqual("Invalid Cards");
expect(getCardValue("11♦")).toEqual("Invalid Cards");
expect(getCardValue("4♥♥")).toEqual("Invalid Cards");
expect(getCardValue("")).toEqual("Invalid Cards");
expect(getCardValue("♠")).toEqual("Invalid Cards");
expect(getCardValue("-9♥")).toEqual("Invalid Cards");
});

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
Expand All @@ -17,4 +41,3 @@ test(`Should return 11 when given an ace card`, () => {
// 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

Loading