Skip to content
Open
Original file line number Diff line number Diff line change
@@ -1,37 +1,61 @@
// Implement a function getAngleType
//
// When given an angle in degrees, it should return a string indicating the type of angle:
// - "Acute angle" for angles greater than 0° and less than 90°
// - "Right angle" for exactly 90°
// - "Obtuse angle" for angles greater than 90° and less than 180°
// - "Straight angle" for exactly 180°
// - "Reflex angle" for angles greater than 180° and less than 360°
// - "Invalid angle" for angles outside the valid range.

// Assumption: The parameter is a valid number. (You do not need to handle non-numeric inputs.)

// Acceptance criteria:
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.

function getAngleType(angle) {
// TODO: Implement this function
// Ensure non-numbers are treated as an invalid angles
if (typeof angle != "number") return "Invalid angle";

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";

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;

// This helper function is written to make our assertions easier to read.
// If the actual output matches the target output, the test will pass
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}

// TODO: Write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles
const right = getAngleType(90);
// Acute Angles Test
let acute = getAngleType(45);
assertEquals(acute, "Acute angle");

// Right Angles Test
let right = getAngleType(90);
assertEquals(right, "Right angle");

// Obtuse Angles Test
let obtuse = getAngleType(120);
assertEquals(obtuse, "Obtuse angle");

// Straight Angles Test
let straight = getAngleType(180);
assertEquals(straight, "Straight angle");

// Reflex Angles Test
let reflex = getAngleType(200);
assertEquals(reflex, "Reflex angle");

// Invalid Angles Test
let invalid = getAngleType(0);
assertEquals(invalid, "Invalid angle");

// Invalid Angles Test
invalid = getAngleType(360);
assertEquals(invalid, "Invalid angle");

// Invalid Angles Test
invalid = getAngleType(500);
assertEquals(invalid, "Invalid angle");

// Invalid Angles Test
invalid = getAngleType(-200);
assertEquals(invalid, "Invalid angle");

// Test for float inputs
acute = getAngleType(34.333);
assertEquals(acute, "Acute angle");
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
// Implement a function isProperFraction,
// when given two numbers, a numerator and a denominator, it should return true if
// the given numbers form a proper fraction, and false otherwise.

// Assumption: The parameters are valid numbers (not NaN or Infinity).

// Note: If you are unfamiliar with proper fractions, please look up its mathematical definition.
function isProperFraction(numerator, denominator) {
// Return non numeric numerators and denominators as false
if (typeof numerator != "number" || typeof denominator != "number")
return false;

// Acceptance criteria:
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.
if (numerator === 0 || denominator === 0) return false;

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

// The line below allows us to load the isProperFraction function into tests in other files.
// This will be useful in the "rewrite tests with jest" step.
module.exports = isProperFraction;

// Here's our helper again
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}

// TODO: Write tests to cover all cases.
// What combinations of numerators and denominators should you test?

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

// Improper proper fraction test
assertEquals(isProperFraction(3, 2), false);

// Negative proper fraction test
assertEquals(isProperFraction(-1, 2), true);

// Zero numerator test
assertEquals(isProperFraction(0, 2), false);
103 changes: 75 additions & 28 deletions Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck

// Implement a function getCardValue, when given a string representing a playing card,
// should return the numerical value of the card.

// A valid card string will contain a rank followed by the suit.
// The rank can be one of the following strings:
// "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"
// The suit can be one of the following emojis:
// "♠", "♥", "♦", "♣"
// For example: "A♠", "2♥", "10♥", "J♣", "Q♦", "K♦".
function getCardValue(card) {
// Ensure that the last char is a suit, otherwise throw an error
const suits = ["♠", "♥", "♦", "♣"];
if (!suits.includes(card.slice(-1))) {
throw new Error("Please add the suit to the card face i.e. '5♥' ");
}

// When the card is an ace ("A"), the function should return 11.
// When the card is a face card ("J", "Q", "K"), the function should return 10.
// When the card is a number card ("2" to "10"), the function should return its numeric value.
const rank = card.slice(0, -1);
const validRank = [
"A",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"J",
"Q",
"K",
];

// When the card string is invalid (not following the above format), the function should
// throw an error.
// Throw an error if an invalid card is entered
if (!validRank.includes(rank)) {
throw new Error("Please enter a valid card face");
}

// Acceptance criteria:
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.
// Return value if card rank is a number
if (parseInt(rank)) {
return parseInt(rank);
}

function getCardValue(card) {
// TODO: Implement this function
// Return 11 for "A" and 10 for "J", "Q", "K"
if (rank === "A") {
return 11;
} else {
return 10;
}
}

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

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

// Handling invalid cards
// ============= Invalid Card Tests ===========
let invalidCard = "invalid";
try {
getCardValue("invalid");
getCardValue(invalidCard);
console.error(`Error was not thrown for an invalid card (${invalidCard})`);
} catch (e) {}

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

invalidCard = "";
try {
getCardValue(invalidCard);
console.error(`Error was not thrown for an invalid card (${invalidCard})`);
} catch (e) {}

// What other invalid card cases can you think of?
invalidCard = "AA♠";
try {
getCardValue(invalidCard);
console.error(`Error was not thrown for an invalid card (${invalidCard})`);
} catch (e) {}

invalidCard = "10♠♦";
try {
getCardValue(invalidCard);
console.error(`Error was not thrown for an invalid card (${invalidCard})`);
} catch (e) {}

invalidCard = "10";
try {
console.log(getCardValue(invalidCard));
getCardValue(invalidCard);
console.error(`Error was not thrown for an invalid card (${invalidCard})`);
} catch (e) {}
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
// 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.

// Case 1: Acute angles
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");
});

// 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)`, () => {
expect(getAngleType(91.0)).toEqual("Obtuse angle");
expect(getAngleType(100)).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)`, () => {
expect(getAngleType(181)).toEqual("Reflex angle");
expect(getAngleType(200)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});

// Case 6: Invalid angles
test(`should return "Invalid angle" when angle doesn't lie between 0-360 exclusive`, () => {
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(500)).toEqual("Invalid angle");
expect(getAngleType(-200)).toEqual("Invalid angle");
expect(getAngleType("")).toEqual("Invalid angle");
expect(getAngleType("ten degrees")).toEqual("Invalid angle");
expect(getAngleType(true)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,58 @@
// This statement loads the isProperFraction function you wrote in the implement directory.
// We will use the same function, but write tests for it using Jest in this file.
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`, () => {
// Case 1: numerator/denominator is zero
test(`should return false when either numerator or denominator is zero`, () => {
expect(isProperFraction(0, 1)).toEqual(false);
expect(isProperFraction(1, 0)).toEqual(false);
expect(isProperFraction(0, 0)).toEqual(false);
});

// Case 2: numerator and denominator are positive integers and form proper fractions
test(`should return true when either numerator is smaller than denominator`, () => {
expect(isProperFraction(1, 2)).toEqual(true);
expect(isProperFraction(1, 10)).toEqual(true);
expect(isProperFraction(9, 100)).toEqual(true);
});

// Case 3: numerator and denominator are negative and make proper fractions
test(`should return true when absolute of numerator is smaller than absolute of denominator`, () => {
expect(isProperFraction(-1, 2)).toEqual(true);
expect(isProperFraction(1, -10)).toEqual(true);
expect(isProperFraction(-9, -100)).toEqual(true);
});

// Case 4: numerator/denominator is decimal and make proper fractions
test(`should return true when decimal numerator is smaller than decimal denominator`, () => {
expect(isProperFraction(0.5, 2)).toEqual(true);
expect(isProperFraction(1.2, -10)).toEqual(true);
expect(isProperFraction(-9, -9.5)).toEqual(true);
});

// Case 5: numerator and denominator are positive integers and not proper fractions
test(`should return false when numerator is greater than denominator`, () => {
expect(isProperFraction(2, 1)).toEqual(false);
expect(isProperFraction(10, 1)).toEqual(false);
expect(isProperFraction(100, 9)).toEqual(false);
});

// Case 6: numerator and denominator are negative and not proper fractions
test(`should return false when absolute of numerator is greater than absolute of denominator`, () => {
expect(isProperFraction(-2, 1)).toEqual(false);
expect(isProperFraction(10, -1)).toEqual(false);
expect(isProperFraction(-100, -9)).toEqual(false);
});

// Case 7: numerator/denominator is decimal and not proper fractions
test(`should return false when numerator is greater than denominator`, () => {
expect(isProperFraction(2.5, 1)).toEqual(false);
expect(isProperFraction(10.2, 1)).toEqual(false);
expect(isProperFraction(100.5, 9.001)).toEqual(false);
});

// Case 8: numerator/denominator is not numeric
test(`should return false when either numerator or denominator is not numeric`, () => {
expect(isProperFraction(false, 1)).toEqual(false);
expect(isProperFraction(true, 1)).toEqual(false);
expect(isProperFraction("10.2", 1)).toEqual(false);
expect(isProperFraction("", 9.001)).toEqual(false);
});
Loading