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

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 if(angle === 0){
return "Zero angle"
}
else if(angle === 360){
return "Complete angle"
}
else{
return "Invalid angle";
}
}

// The line below allows us to load the getAngleType function into tests in other files.
Expand All @@ -32,6 +56,32 @@ function assertEquals(actualOutput, 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 for right angles
assertEquals(getAngleType(90), "Right angle");

// Tests for acute angles
assertEquals(getAngleType(56), "Acute angle");
assertEquals(getAngleType(1), "Acute angle");

// Test for a straight line
assertEquals(getAngleType(180), "Straight angle");

// Test for obtuse angle
assertEquals(getAngleType(95), "Obtuse angle");
assertEquals(getAngleType(160), "Obtuse angle");
assertEquals(getAngleType(102), "Obtuse angle");

// Test for reflex angle
assertEquals(getAngleType(181), "Reflex angle");
assertEquals(getAngleType(249), "Reflex angle");

// Test for invalid angles
assertEquals(getAngleType(-980), "Invalid angle");
assertEquals(getAngleType(9082), "Invalid angle");
assertEquals(getAngleType(672), "Invalid angle");
//
assertEquals(getAngleType(0), "Zero angle");
assertEquals(getAngleType(360), "Complete angle");
console.log("Execution finished! If any test failed, console.assert errors will appear above.");

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// execute the code to ensure all tests pass.

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

Expand All @@ -31,3 +32,11 @@ function assertEquals(actualOutput, targetOutput) {

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(93, 112), true);
assertEquals(isProperFraction(2, 1), false);
assertEquals(isProperFraction(80, 80), false);
assertEquals(isProperFraction(-10, -15), true);
assertEquals(isProperFraction(-35, 25), false);
assertEquals(isProperFraction(1, 0), false);
assertEquals(isProperFraction(0, 1), true);
console.log("Execution finished! If any test failed, console.assert errors will appear above.");
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,44 @@

function getCardValue(card) {
// TODO: Implement this function
if (typeof card !== "string" || card.length < 2 || card.length > 3) {
throw new Error("Invalid card");
}
const ranks = [
"A",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"J",
"Q",
"K",
];
const suits = ["♠", "♥", "♦", "♣"];
let rank = card.slice(0, -1)// This is extracting the bit before the suits alone
let suit = card.slice(-1) // THis is extracting the suit in the string

if(!ranks.includes(rank)){
throw new Error("Invalid card")
}
if (!suits.includes(suit)){
throw new Error("Invalid card")
}
if (rank === "A"){
return 11}
if (["J", "K", "Q"].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,6 +76,20 @@ 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("5♣"), 5);
assertEquals(getCardValue("2♣"), 2);
assertEquals(getCardValue("10♥"), 10);
assertEquals(getCardValue("6♥"), 6);
assertEquals(getCardValue("5♥"), 5);
assertEquals(getCardValue("8♥"), 8);
assertEquals(getCardValue("4♥"), 4);
assertEquals(getCardValue("3♥"), 3);
assertEquals(getCardValue("Q♦"), 10);
assertEquals(getCardValue("7♥"), 7);


// Handling invalid cards
try {
Expand All @@ -52,3 +102,17 @@ try {
}

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

try {
getCardValue("A");
console.error("Error not thrown for invalid card missing suit");
} catch (e) {
console.log("Error thrown for invalid card missing suit🎉");
}

try {
getCardValue("");
console.error("Error not thrown for invalid card containing empty string");
} catch (e) {
console.log("Error thrown for invalid card containing empty string");
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,38 @@ 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(90.1)).toEqual("Obtuse angle");
expect(getAngleType(179.9)).toEqual("Obtuse angle");
expect(getAngleType(99.8)).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(256)).toEqual("Reflex angle");
expect(getAngleType(359.9)).toEqual("Reflex angle");
});
// Case 6: Invalid angles
test(`should return "Invalid angle" when (angle > 360 || angle < 0)`, () => {
// Test various invalid angles
expect(getAngleType(-180)).toEqual("Invalid angle");
expect(getAngleType(561)).toEqual("Invalid angle");
expect(getAngleType(980)).toEqual("Invalid angle");
});
test(`should return "Zero angle" when (angle === 0)`, () => {
expect(getAngleType(0)).toEqual("Zero angle");
});
test(`should return "Zero angle" when (angle === 360)`, () => {
expect(getAngleType(360)).toEqual("Complete angle");
});

Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,21 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});
// proper fractions
test(`should return true when denominator is higher than numerator`, () => {
expect(isProperFraction(0, 1)).toEqual(true);
expect(isProperFraction(2, 7)).toEqual(true);
expect(isProperFraction(89, 101)).toEqual(true);
});
// Special case: numerator or denominator is negative, we consider the absolute values for fractions so ignore the negative signs
test(`should return true when negative/positive numerator is less than the positive/negative denominator`, () => {
expect(isProperFraction(-20, -30)).toEqual(true);
expect(isProperFraction(-1, 2)).toEqual(true);
expect(isProperFraction(58, -68)).toEqual(true);
});
// Special case: numerator or denominator is negative, we consider the absolute values for fractions so ignore the negative signs
test(`should return false when negative/positive numerator is greater than the positive/negative denominator`, () => {
expect(isProperFraction(-50, 10)).toEqual(false);
expect(isProperFraction(100, 2)).toEqual(false);
expect(isProperFraction(-1, -0)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,35 @@
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);
});

//Case 2: 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("J♦")).toEqual(10);
});
//case 3: Number Cards (2-10)
test(`Should return the number when given a number card`, () => {
expect(getCardValue("4♠")).toEqual(4);
expect(getCardValue("9♣")).toEqual(9);
expect(getCardValue("2♦")).toEqual(2);
});
//Case 4: Invalid cards
test(`Should return the invalid suit, invalid card or invalid card`, () => {
expect(() => getCardValue("Apple")).toThrow("Invalid card");
expect(() => getCardValue("4🎉")).toThrow("Invalid card");
expect(() => getCardValue("20♣")).toThrow("Invalid card");
});
// 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
// https://jestjs.io/docs/expecttothrowerror

Loading