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,11 +16,29 @@

function getAngleType(angle) {
// TODO: Implement this function
// You can use if-else statements or a switch statement to determine the type of angle based on the input value.
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.
// This will be useful in the "rewrite tests with jest" step.
module.exports = getAngleType;
// You can run this file with node to check your implementation, but you will need to write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles
const right = getAngleType(90);
console.log(right); // Output: "Right angle"

// This helper function is written to make our assertions easier to read.
// If the actual output matches the target output, the test will pass
Expand All @@ -31,7 +49,26 @@ 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");
// Example: Identify Acute Angles
const acute = getAngleType(45);
assertEquals(acute, "Acute angle");
// Example: Identify Obtuse Angles
const obtuse = getAngleType(120);
assertEquals(obtuse, "Obtuse angle");
// Example: Identify Straight Angles
const straight = getAngleType(180);
assertEquals(straight, "Straight angle");
// Example: Identify Reflex Angles
const reflex = getAngleType(270);
assertEquals(reflex, "Reflex angle");
// Example: Identify Invalid Angles
const invalid = getAngleType(-10);
assertEquals(invalid, "Invalid angle");
const invalid2 = getAngleType(360);
assertEquals(invalid2, "Invalid 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
// A proper fraction is a fraction where the absolute value of the numerator is less than the absolute value of the denominator. Additionally, the denominator cannot be zero. Therefore, we can check if the absolute value of the numerator is less than the absolute value of the denominator and if the denominator is not zero to determine if it is a proper fraction.
if (denominator === 0) {
return false; // A fraction cannot have a denominator of zero
}
return Math.abs(numerator) < Math.abs(denominator);
}

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

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
// Example: 2/1 is not a proper fraction
assertEquals(isProperFraction(2, 1), false);
// Example: -1/2 is a proper fraction
assertEquals(isProperFraction(-1, 2), true);
// Example: 1/-2 is a proper fraction
assertEquals(isProperFraction(1, -2), true);
// Example: -1/-2 is a proper fraction
assertEquals(isProperFraction(-1, -2), true);
// Example: 0/5 is a proper fraction
assertEquals(isProperFraction(0, 5), true);
// Example: 5/0 is not a proper fraction
assertEquals(isProperFraction(5, 0), false);
// Example: 5/5 is not a proper fraction
assertEquals(isProperFraction(5, 5), false);

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@

function getCardValue(card) {
// TODO: Implement this function
const rank = card.slice(0, -1); // Get the rank by slicing off the last character (the suit)
const suit = card.slice(-1); // Get the suit by taking the last character

// Define valid ranks and suits
const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
const validSuits = ["♠", "♥", "♦", "♣"];

// Check if the rank and suit are valid
if (!validRanks.includes(rank) || !validSuits.includes(suit)) {
throw new Error("Invalid card");
}

// Determine the value based on the rank
if (rank === "A") {
return 11;
} else if (["J", "Q", "K"].includes(rank)) {
return 10;
} else {
return parseInt(rank); // Convert number cards to their numeric value
}

}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -40,6 +61,12 @@ 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("Q♣"), 10);
assertEquals(getCardValue("K♠"), 10);

// You should also test the boundary cases, such as "10♥", and invalid cases, such as "11♠" or "A" or "♠".

// Handling invalid cards
try {
Expand All @@ -49,4 +76,25 @@ try {
console.error("Error was not thrown for invalid card");
} catch (e) {}


// What other invalid card cases can you think of?
try {
getCardValue("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) {}

try {
getCardValue("A");

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

try {
getCardValue("♠");

// This line will not be reached if an error is thrown as expected
console.error("Error was not thrown for invalid card");
} catch (e) {}
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
// This statement loads the getAngleType function you wrote in the implement directory.
// We will use the same function, but write tests for it using Jest in this file.
const getAngleType = require("../implement/1-get-angle-type");
//

// TODO: Write tests in Jest syntax to cover all cases/outcomes,
// including boundary and invalid cases.
// Example: Identify Right Angles
test(`should return "Right angle" when angle is 90`, () => {
expect(getAngleType(90)).toEqual("Right angle");
});


// Case 1: Acute angles
test(`should return "Acute angle" when (0 < angle < 90)`, () => {
Expand All @@ -18,3 +24,4 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
// Case 4: Straight angle
// Case 5: Reflex angles
// Case 6: Invalid angles

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,42 @@
const isProperFraction = require("../implement/2-is-proper-fraction");

// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.
// Example: 1/2 is a proper fraction
test(`should return true for 1/2`, () => {
expect(isProperFraction(1, 2)).toEqual(true);
});
// Example: 2/1 is not a proper fraction
test(`should return false for 2/1`, () => {
expect(isProperFraction(2, 1)).toEqual(false);
});
// Example: -1/2 is a proper fraction
test(`should return true for -1/2`, () => {
expect(isProperFraction(-1, 2)).toEqual(true);
});
// Example: 1/-2 is a proper fraction
test(`should return true for 1/-2`, () => {
expect(isProperFraction(1, -2)).toEqual(true);
});
// Example: -1/-2 is a proper fraction
test(`should return true for -1/-2`, () => {
expect(isProperFraction(-1, -2)).toEqual(true);
});
// Example: 0/5 is a proper fraction
test(`should return true for 0/5`, () => {
expect(isProperFraction(0, 5)).toEqual(true);
});
// Example: 5/0 is not a proper fraction
test(`should return false for 5/0`, () => {
expect(isProperFraction(5, 0)).toEqual(false);
});
// Example: 5/5 is not a proper fraction
test(`should return false for 5/5`, () => {
expect(isProperFraction(5, 5)).toEqual(false);
});
// Special case: numerator is zero
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});

// Special case: numerator is zero
test(`should return false when denominator is zero`, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@
const getCardValue = require("../implement/3-get-card-value");

// TODO: Write tests in Jest syntax to cover all possible outcomes.
// Examples:
test(`should return 9 for "9♠"`, () => {
expect(getCardValue("9♠")).toEqual(9);
});

test(`should return 11 for "A♥"`, () => {
expect(getCardValue("A♥")).toEqual(11);
});

test(`should return 10 for "J♦"`, () => {
expect(getCardValue("J♦")).toEqual(10);
});

test(`should return 10 for "Q♣"`, () => {
expect(getCardValue("Q♣")).toEqual(10);
});

test(`should return 10 for "K♠"`, () => {
expect(getCardValue("K♠")).toEqual(10);
});


// Case 1: Ace (A)
test(`Should return 11 when given an ace card`, () => {
Expand All @@ -14,6 +35,7 @@ test(`Should return 11 when given an ace card`, () => {
// 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
Expand Down