Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@

function getAngleType(angle) {
// TODO: Implement this function
if ( angle <=0 || angle >= 360 ) {
return "Invalid angle";
}
if ( 0 < angle && angle < 90 ){
return "Acute angle";
}
if (angle === 90){
return "Right angle";
}
if ( 90 < angle && angle < 180){
return "Obtuse angle";
}
if (angle === 180){
return "Straight angle";
}
return "Reflex angle";
}

// The line below allows us to load the getAngleType function into tests in other files.
Expand All @@ -35,3 +51,13 @@ function assertEquals(actualOutput, targetOutput) {
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");
const acute = getAngleType(45);
assertEquals(acute, "Acute angle");
const obtuse = getAngleType(120);
assertEquals(obtuse, "Obtuse angle");
const straight = getAngleType(180);
assertEquals(straight, "Straight angle");
const reflex = getAngleType(270);
assertEquals(reflex, "Reflex angle");
const zero = getAngleType(0);
assertEquals(zero, "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
return numerator >= 0 && denominator > 0 && numerator < denominator;
}

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

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

// Proper fractions - should return true
assertEquals(isProperFraction(3, 4), true);
assertEquals(isProperFraction(1, 5), true);
assertEquals(isProperFraction(2, 3), true);
assertEquals(isProperFraction(5, 8), true);
assertEquals(isProperFraction(1, 100), true);

// Improper fractions - should return false
assertEquals(isProperFraction(2, 1), false);
assertEquals(isProperFraction(5, 3), false);
assertEquals(isProperFraction(4, 4), false);
assertEquals(isProperFraction(10, 5), false);
assertEquals(isProperFraction(100, 99), false);

// Edge cases with zero numerator - should return true
assertEquals(isProperFraction(0, 5), true);
assertEquals(isProperFraction(0, 1), true);

// Negative numbers - should return false
assertEquals(isProperFraction(-1, 2), false);
assertEquals(isProperFraction(1, -2), false);
assertEquals(isProperFraction(-1, -2), false);
132 changes: 118 additions & 14 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,31 +22,135 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
// Define valid ranks and suits
const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
const validSuits = ["♠", "♥", "♦", "♣"];

// Check if card is a valid string
if (typeof card !== 'string' || card.length === 0) {
throw new Error('Invalid card');
}

// Extract suit (last character)
const suit = card[card.length - 1];

// Extract rank (everything except last character)
const rank = card.slice(0, -1);

// Validate suit and rank
if (!validSuits.includes(suit) || !validRanks.includes(rank)) {
throw new Error('Invalid card');
}

// Return value based on rank
if (rank === "A") {
return 11;
} else if (rank === "J" || rank === "Q" || rank === "K") {
return 10;
} else {
return parseInt(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;

// Helper functions to make our assertions easier to read.
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}
// Only run tests if this file is executed directly (not imported as a module)
if (require.main === module) {
// Helper functions to make our assertions easier to read.
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}

// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.

// Test all Ace cards (should return 11)
assertEquals(getCardValue("A♠"), 11);
assertEquals(getCardValue("A♥"), 11);
assertEquals(getCardValue("A♦"), 11);
assertEquals(getCardValue("A♣"), 11);

// Test all face cards (should return 10)
assertEquals(getCardValue("J♠"), 10);
assertEquals(getCardValue("J♥"), 10);
assertEquals(getCardValue("J♦"), 10);
assertEquals(getCardValue("J♣"), 10);
assertEquals(getCardValue("Q♠"), 10);
assertEquals(getCardValue("Q♥"), 10);
assertEquals(getCardValue("Q♦"), 10);
assertEquals(getCardValue("Q♣"), 10);
assertEquals(getCardValue("K♠"), 10);
assertEquals(getCardValue("K♥"), 10);
assertEquals(getCardValue("K♦"), 10);
assertEquals(getCardValue("K♣"), 10);

// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:
// Test number cards (should return their numeric value)
assertEquals(getCardValue("2♠"), 2);
assertEquals(getCardValue("3♣"), 3);
assertEquals(getCardValue("4♥"), 4);
assertEquals(getCardValue("5♦"), 5);
assertEquals(getCardValue("6♠"), 6);
assertEquals(getCardValue("7♣"), 7);
assertEquals(getCardValue("8♥"), 8);
assertEquals(getCardValue("9♠"), 9);
assertEquals(getCardValue("10♥"), 10);

// Handling invalid cards
// Handling invalid cards - generic invalid string
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?
// Card without suit
try {
getCardValue("A");
console.error("Error was not thrown for card without suit");
} catch (e) {}

// Card without rank (just a suit)
try {
getCardValue("♠");
console.error("Error was not thrown for card without rank");
} catch (e) {}

// Invalid rank
try {
getCardValue("11♠");
console.error("Error was not thrown for invalid rank '11'");
} catch (e) {}

try {
getCardValue("X♠");
console.error("Error was not thrown for invalid rank 'X'");
} catch (e) {}

// Invalid suit
try {
getCardValue("A♪");
console.error("Error was not thrown for invalid suit");
} catch (e) {}

try {
getCardValue("AH");
console.error("Error was not thrown for invalid suit 'H'");
} catch (e) {}

// Empty string
try {
getCardValue("");
console.error("Error was not thrown for empty string");
} catch (e) {}

// Extra characters
try {
getCardValue("A♠X");
console.error("Error was not thrown for extra characters");
} catch (e) {}

console.log("All tests completed!");

} // End of: if (require.main === module)
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(91)).toEqual("Obtuse angle");
expect(getAngleType(135)).toEqual("Obtuse angle");
expect(getAngleType(179)).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(270)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test(`should return "Invalid angle" for angles outside valid range`, () => {
// Test boundary and invalid cases
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(-1)).toEqual("Invalid angle");
expect(getAngleType(-90)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(361)).toEqual("Invalid angle");
expect(getAngleType(1000)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,43 @@ 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
// Proper fractions - should return true
test(`should return true for proper fractions (0 <= numerator < denominator)`, () => {
expect(isProperFraction(1, 2)).toEqual(true);
expect(isProperFraction(3, 4)).toEqual(true);
expect(isProperFraction(1, 5)).toEqual(true);
expect(isProperFraction(2, 3)).toEqual(true);
expect(isProperFraction(5, 8)).toEqual(true);
expect(isProperFraction(1, 100)).toEqual(true);
expect(isProperFraction(0, 5)).toEqual(true);
});

// Improper fractions - should return false
test(`should return false for improper fractions (numerator >= denominator)`, () => {
expect(isProperFraction(2, 1)).toEqual(false);
expect(isProperFraction(5, 3)).toEqual(false);
expect(isProperFraction(4, 4)).toEqual(false);
expect(isProperFraction(10, 5)).toEqual(false);
expect(isProperFraction(100, 99)).toEqual(false);
});

// Edge cases with zero numerator - should return true
test(`should return true when numerator is zero and denominator is positive`, () => {
expect(isProperFraction(0, 5)).toEqual(true);
expect(isProperFraction(0, 1)).toEqual(true);
expect(isProperFraction(0, 100)).toEqual(true);
});

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

// Negative numbers - should return false
test(`should return false for negative numbers`, () => {
expect(isProperFraction(-1, 2)).toEqual(false);
expect(isProperFraction(1, -2)).toEqual(false);
expect(isProperFraction(-1, -2)).toEqual(false);
expect(isProperFraction(-5, -3)).toEqual(false);
});
Loading