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 @@ -14,8 +14,23 @@
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.

//Question:
// What about 360 angle that is a valid angle and called complete angle should I include that as well?
function getAngleType(angle) {
// TODO: Implement this function
if (angle <= 0 || angle >= 360) {
return "Invalid angle";
} else if (angle < 90) {
return "Acute angle";
} else if (angle === 90) {
return "Right angle";
} else if (angle < 180) {
return "Obtuse angle";
} else if (angle === 180) {
return "Straight angle";
} else if (angle < 360) {
return "Reflex angle";
}
}

// The line below allows us to load the getAngleType function into tests in other files.
Expand All @@ -33,5 +48,27 @@ 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");
let angle = getAngleType(0);
assertEquals(angle, "Invalid angle");
angle = getAngleType(-78);
assertEquals(angle, "Invalid angle");
angle = getAngleType(1);
assertEquals(angle, "Acute angle");
angle = getAngleType(90);
assertEquals(angle, "Right angle");
angle = getAngleType(91);
assertEquals(angle, "Obtuse angle");
angle = getAngleType(180);
assertEquals(angle, "Straight angle");
angle = getAngleType(181);
assertEquals(angle, "Reflex angle");
angle = getAngleType(360);
assertEquals(angle, "Invalid angle");
angle = getAngleType(45671);
assertEquals(angle, "Invalid angle");
angle = getAngleType(0.5);
assertEquals(angle, "Acute angle");
angle = getAngleType(90.0098);
assertEquals(angle, "Obtuse angle");
angle = getAngleType(-0.0001);
assertEquals(angle, "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

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

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

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(2, 2), false);
assertEquals(isProperFraction(3, 2), false);
assertEquals(isProperFraction(0, 2), true);
assertEquals(isProperFraction(-1, 2), true);
assertEquals(isProperFraction(-1, -2), true);
assertEquals(isProperFraction(1, -2), true);
assertEquals(isProperFraction(0, -1), true);
assertEquals(isProperFraction(100, -100), false);
assertEquals(isProperFraction(-100, 100), false);
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,34 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
// Handling invalid cards
const rank = card.slice(0, -1); //rank of the card is everything except the last character of card string
const suit = card.slice(-1); // suit is the last character of the card string
if (!isValidCard(rank, suit)) throw new Error("Invalid Card");
if (rank === "J" || rank === "Q" || rank == "K") return 10;
else if (rank == "A") return 11;
else return Number(rank);
}

function isValidCard(rank, suit) {
const ranks = [
"A",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"J",
"Q",
"K",
];
const suits = ["♠", "♥", "♦", "♣"];
if (ranks.includes(rank) && suits.includes(suit)) return true;
else return false;
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -37,16 +64,29 @@ function assertEquals(actualOutput, targetOutput) {
);
}

function assertThrows(fnGetCardValue) {
try {
fnGetCardValue(); // run the function
console.assert(false, "Expected function to throw an error");
} catch (err) {
console.assert(err instanceof Error, "Expected an Error to be thrown");
}
}

// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:
assertEquals(getCardValue("9♠"), 9);
assertEquals(getCardValue("10♠"), 10);
assertEquals(getCardValue("Q♠"), 10);
assertEquals(getCardValue("A♣"), 11);
assertEquals(getCardValue("2♠"), 2);
assertEquals(getCardValue("J♦"), 10);
assertEquals(getCardValue("K♠"), 10);

// Handling invalid cards
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) {}

// What other invalid card cases can you think of?
// using function wrapper in the below lines of code so instead of passing teh result of function I can pass the function here,
// and it is being called in try block of assertThrows function
assertThrows(() => getCardValue("Invalid"));
assertThrows(() => getCardValue("1Q"));
assertThrows(() => getCardValue("-10♦"));
assertThrows(() => getCardValue("♦K"));
assertThrows(() => getCardValue("Q♦♦"));
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 angle" when angle = 90)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(90)).toEqual("Right angle");
});

// Case 3: Obtuse angles
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(91)).toEqual("Obtuse angle");
expect(getAngleType(112)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
});

// Case 4: Straight angle
test(`should return "Straight angle" when angle = 180`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(180)).toEqual("Straight angle");
});

// Case 5: Reflex angles
test(`should return "Reflex angle" when (180 < angle < 360)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(181)).toEqual("Reflex angle");
expect(getAngleType(223)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test(`should return "Invalid angle" when angle >= 360 or angle <= 0)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(-1)).toEqual("Invalid angle");
expect(getAngleType(-2292)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(365)).toEqual("Invalid angle");
expect(getAngleType(383949)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,40 @@ 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.

// Special case: numerator is zero
//Case 1: denominator is zero
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
expect(isProperFraction(-1, 0)).toEqual(false);
expect(isProperFraction(0, 0)).toEqual(false);
expect(isProperFraction(3848473, 0)).toEqual(false);
});

//Case 2: numerator is 0 but denominator is not 0
test(`should return true when numerator is zero but denominator is not 0`, () => {
expect(isProperFraction(0, 1)).toEqual(true);
expect(isProperFraction(0, -100)).toEqual(true);
expect(isProperFraction(0, 1000)).toEqual(true);
});

//Case 3: Absolute values of numerator and denominator are equal
test(`should return false when numerator and denominator have equal absolute values`, () => {
expect(isProperFraction(1, 1)).toEqual(false);
expect(isProperFraction(-181, 181)).toEqual(false);
expect(isProperFraction(839984, 839984)).toEqual(false);
expect(isProperFraction(-1211, -1211)).toEqual(false);
});

//Case 4: Absolute value of numerator is less than absolute value of denominator
test(`should return true when absolute numerator is less than absolute denominator`, () => {
expect(isProperFraction(1, 2)).toEqual(true);
expect(isProperFraction(1, -2)).toEqual(true);
expect(isProperFraction(-2, -6)).toEqual(true);
expect(isProperFraction(-0, -1000)).toEqual(true);
});

//Case 5: Absolute value of numerator is greater than absolute value of denominator
test(`should return false when absolute numerator is greater than absolute denominator`, () => {
expect(isProperFraction(-6, -2)).toEqual(false);
expect(isProperFraction(5, 3)).toEqual(false);
expect(isProperFraction(-25, 3)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,48 @@ test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
});

// Case 2: Number Cards (2-10)
test(`Should return number when given a number card`, () => {
expect(getCardValue("2♠")).toEqual(2);
expect(getCardValue("10♠")).toEqual(10);
expect(getCardValue("5♠")).toEqual(5);
});

// Case 3: Face Cards (J, Q, K)
test(`Should return 10 when given a face card`, () => {
expect(getCardValue("Q♠")).toEqual(10);
expect(getCardValue("K♠")).toEqual(10);
expect(getCardValue("J♠")).toEqual(10);
});

// Case 4: Invalid Cards
test(`Should return Error when given an invalid card`, () => {
expect(() => {
getCardValue("Aas♠");
}).toThrow("Invalid Card");
expect(() => {
getCardValue("What");
}).toThrow("Invalid Card");
expect(() => {
getCardValue("Q10");
}).toThrow();
expect(() => {
getCardValue("11");
}).toThrow("Invalid Card");
expect(() => {
getCardValue("♠11");
}).toThrow("Invalid Card");
expect(() => {
getCardValue("10*");
}).toThrow("Invalid Card");
expect(() => {
getCardValue("Q_");
}).toThrow("Invalid Card");
expect(() => {
getCardValue("A10");
}).toThrow("Invalid Card");
});

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
// Face Cards (J, Q, K)
Expand All @@ -17,4 +59,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