Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@
// execute the code to ensure all tests pass.

function getAngleType(angle) {
// TODO: Implement this function
if (angle > 0 && angle < 90) return "Acute angle";
if (angle === 90) return "Right angle";
if (angle > 90 && angle < 180) return "Obtuse angle";
if (angle === 180) return "Straight angle";
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.
Expand All @@ -35,3 +40,18 @@ function assertEquals(actualOutput, targetOutput) {
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");

const straight = getAngleType(180);
assertEquals(straight, "Straight angle");

const acute = getAngleType(47);
assertEquals(acute, "Acute angle");

const obtuse = getAngleType(139);
assertEquals(obtuse, "Obtuse angle");

const reflex = getAngleType(258);
assertEquals(reflex, "Reflex angle");

const invalid = getAngleType(360);
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
// console.log(numerator, denominator);
// console.log(Math.abs(numerator), Math.abs(denominator));
if (denominator !== 0 && 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 +37,21 @@ function assertEquals(actualOutput, targetOutput) {

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);

assertEquals(isProperFraction(9, 10), true);

assertEquals(isProperFraction(-6, 8), true);

assertEquals(isProperFraction(-2, -3), true);

assertEquals(isProperFraction(3.5, 6), true);

assertEquals(isProperFraction(4, 4.67), true);

assertEquals(isProperFraction(7, 0), false);

assertEquals(isProperFraction(2, 1), false);

assertEquals(isProperFraction(-9, -3), false);

assertEquals(isProperFraction(-5.68, 0), false);
175 changes: 173 additions & 2 deletions Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,43 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
const validSuits = ["♠", "♥", "♦", "♣"];
if (typeof card !== "string")
throw new Error(`Invalid card format: "${card}". Expected a string.`);

let cardSuit = card.slice(-1);
let cardRank = card.slice(0, -1);

if (validSuits.includes(cardSuit)) {
switch (cardRank) {
case "A":
return 11;

case "J":
case "Q":
case "K":
return 10;

case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
case "10":
return Number(cardRank);

default:
throw new Error(
`Invalid card format: "${card}" has an invalid rank "${cardRank}". Expected one of: A, 2–10, J, Q, K.`
);
}
} else
throw new Error(
`Invalid card format: "${card}" has an invalid suit "${cardSuit}". Expected one of: ♠, ♥, ♦, ♣.`
);
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -41,12 +77,147 @@ function assertEquals(actualOutput, targetOutput) {
// Examples:
assertEquals(getCardValue("9♠"), 9);

assertEquals(getCardValue("4♣"), 4);

assertEquals(getCardValue("A♥"), 11);

assertEquals(getCardValue("J♦"), 10);

assertEquals(getCardValue("Q♣"), 10);

assertEquals(getCardValue("K♠"), 10);

assertEquals(getCardValue("K♦"), 10);

assertEquals(getCardValue("10♥"), 10);

assertEquals(getCardValue("2♦"), 2);

// 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) {}
} catch (err) {
console.error(err);
}

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

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 (err) {
console.error(err);
}

try {
getCardValue("1♥");

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

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 (err) {
console.error(err);
}

try {
getCardValue("0♦");

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

try {
getCardValue("4.8♣");

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

try {
getCardValue("G♦");

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

try {
getCardValue("9");

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

try {
getCardValue("K");

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

try {
getCardValue("9M");

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

try {
getCardValue("♠3");

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

try {
getCardValue(6);

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

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 (err) {
console.error(err);
}

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 (err) {
console.error(err);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,42 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(1)).toEqual("Acute angle");
expect(getAngleType(45)).toEqual("Acute angle");
expect(getAngleType(89)).toEqual("Acute angle");
expect(getAngleType(89.436)).toEqual("Acute angle");
});

// 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 acute angles, including boundary cases
expect(getAngleType(92)).toEqual("Obtuse angle");
expect(getAngleType(111.1)).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(294)).toEqual("Reflex angle");
expect(getAngleType(357.34)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test(`should return "Invalid angle" when (angle <= 0 || angle >= 360)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(-90)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(411.65)).toEqual("Invalid angle");
expect(getAngleType(256578)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,45 @@ 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
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);

test(`should return true when the absolute value of the numerator is less than the absolute value of the denominator and the denominator is not 0`, () => {
expect(isProperFraction(1, 2)).toEqual(true);
expect(isProperFraction(1, -3)).toEqual(true);
expect(isProperFraction(3.5, 4)).toEqual(true);
expect(isProperFraction(-99, -100)).toEqual(true);
expect(isProperFraction(8, 12.5)).toEqual(true);
expect(isProperFraction(-6, 12)).toEqual(true);
expect(isProperFraction(0.03, 0.1)).toEqual(true);
});

test(`should return true when numerator is equal to 0 and denominator is either positive or negative`, () => {
expect(isProperFraction(0, -5)).toEqual(true);
expect(isProperFraction(0, 56)).toEqual(true);
expect(isProperFraction(0, 0.1)).toEqual(true);
expect(isProperFraction(0, -8.9)).toEqual(true);
});

test(`should return false when the denominator is equal to zero`, () => {
expect(isProperFraction(-1, 0)).toEqual(false);
expect(isProperFraction(0, 0)).toEqual(false);
expect(isProperFraction(15, 0)).toEqual(false);
expect(isProperFraction(6.4, 0)).toEqual(false);
expect(isProperFraction(-8.5, 0)).toEqual(false);
});

test(`should return false when the absolute value of the numerator is greater than the absolute value of the denominator`, () => {
expect(isProperFraction(3, 2)).toEqual(false);
expect(isProperFraction(-9, 3)).toEqual(false);
expect(isProperFraction(-15, -4)).toEqual(false);
expect(isProperFraction(36, -5)).toEqual(false);
expect(isProperFraction(3.8, 2.7)).toEqual(false);
expect(isProperFraction(1, 0.7)).toEqual(false);
});

test(`should return false when the absolute value of the numerator is equal to the absolute value of the denominator`, () => {
expect(isProperFraction(3, 3)).toEqual(false);
expect(isProperFraction(-19, -19)).toEqual(false);
expect(isProperFraction(7.45, 7.45)).toEqual(false);
expect(isProperFraction(67, -67)).toEqual(false);
expect(isProperFraction(-8, 8)).toEqual(false);
});
Loading