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
21 changes: 18 additions & 3 deletions Sprint-3/1-key-implement/1-get-angle-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
// Then, write the next test! :) Go through this process until all the cases are implemented

function getAngleType(angle) {
if (angle === 90) return "Right angle";
// read to the end, complete line 36, then pass your test here
if (angle === 90) return "Right angle";
else if (angle < 90 && angle > 0) return "Acute 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 if (angle === 0 || angle === 360) return "Full rotation";
else return "Invalid angle";
}

// we're going to use this helper function to make our assertions easier to read
Expand Down Expand Up @@ -43,14 +48,24 @@ assertEquals(acute, "Acute angle");
// When the angle is greater than 90 degrees and less than 180 degrees,
// Then the function should return "Obtuse angle"
const obtuse = getAngleType(120);
assertEquals(obtuse, "Obtuse angle");
// ====> write your test here, and then add a line to pass the test in the function above

// Case 4: Identify Straight Angles:
// When the angle is exactly 180 degrees,
// Then the function should return "Straight angle"
const straight = getAngleType(180);
assertEquals(straight, "Straight angle");
// ====> write your test here, and then add a line to pass the test in the function above

// Case 5: Identify Reflex Angles:
// When the angle is greater than 180 degrees and less than 360 degrees,
// Then the function should return "Reflex angle"
// ====> write your test here, and then add a line to pass the test in the function above
const reflex = getAngleType(340);
assertEquals(reflex, "Reflex angle");
// ====> write your test here, and then add a line to pass the test in the function above
const zero = getAngleType(0);
assertEquals(zero, "Full rotation");

const invalid = getAngleType(-90);
assertEquals(invalid, "Invalid angle");
9 changes: 8 additions & 1 deletion Sprint-3/1-key-implement/2-is-proper-fraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
// write one test at a time, and make it pass, build your solution up methodically

function isProperFraction(numerator, denominator) {
if (numerator < denominator) return true;
if (typeof numerator !== "number" || typeof numerator !== "number")
return "Invalid Input";
else if (numerator < denominator) return true;
else return false;
}

// here's our helper again
Expand Down Expand Up @@ -40,14 +43,18 @@ assertEquals(improperFraction, false);
// target output: true
// Explanation: The fraction -4/7 is a proper fraction because the absolute value of the numerator (4) is less than the denominator (7). The function should return true.
const negativeFraction = isProperFraction(-4, 7);
assertEquals(negativeFraction, true);
// ====> complete with your assertion

// Equal Numerator and Denominator check:
// Input: numerator = 3, denominator = 3
// target output: false
// Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false.
const equalFraction = isProperFraction(3, 3);
assertEquals(equalFraction, false);
// ====> complete with your assertion

// Stretch:
// What other scenarios could you test for?
const invalid = isProperFraction(undefined, null);
assertEquals(invalid, "Invalid Input");
50 changes: 44 additions & 6 deletions Sprint-3/1-key-implement/3-get-card-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
// write one test at a time, and make it pass, build your solution up methodically
// just make one change at a time -- don't rush -- programmers are deep and careful thinkers
function getCardValue(card) {
if (rank === "A") return 11;
if (typeof card !== "string") throw new Error("Invaid card rank");
const cardOfTen = 10;
const rank = card.slice(0, -1);
if (rank === "A") return 11;
if (rank === "10") return cardOfTen;
if (rank >= 2 && rank <= 9) return parseInt(rank);
if (rank === "J" || rank === "Q" || rank === "K") return cardOfTen;
else throw new Error("Invalid card rank");
}

// You need to write assertions for your function to check it works in different cases
Expand All @@ -25,21 +32,16 @@ function assertEquals(actualOutput, targetOutput) {
// Given a card string in the format "A♠" (representing a card in blackjack - the last character will always be an emoji for a suit, and all characters before will be a number 2-10, or one letter of J, Q, K, A),
// When the function getCardValue is called with this card string as input,
// Then it should return the numerical card value
const aceofSpades = getCardValue("A♠");
assertEquals(aceofSpades, 11);

// Handle Number Cards (2-10):
// Given a card with a rank between "2" and "9",
// When the function is called with such a card,
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
const fiveofHearts = getCardValue("5♥");
// ====> write your test here, and then add a line to pass the test in the function above

// Handle Face Cards (J, Q, K):
// Given a card with a rank of "10," "J," "Q," or "K",
// When the function is called with such a card,
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.

// Handle Ace (A):
// Given a card with a rank of "A",
// When the function is called with an Ace,
Expand All @@ -49,3 +51,39 @@ const fiveofHearts = getCardValue("5♥");
// Given a card with an invalid rank (neither a number nor a recognized face card),
// When the function is called with such a card,
// Then it should throw an error indicating "Invalid card rank."
// Test for Invalid Card
// TEST CASES:
// Test for ace
const aceofSpades = getCardValue("A♠");
assertEquals(aceofSpades, 11);

// Test for "5" card
const fiveofHearts = getCardValue("5♥");
assertEquals(fiveofHearts, 5);

//Test for "10" card:
const tenofClubs = getCardValue("10♣");
assertEquals(tenofClubs, 10);

//Test for Face Cards (J, Q, K)
let rankTen = getCardValue("J♥");
rankTen = getCardValue("Q♥");
assertEquals(rankTen, 10);
rankTen = getCardValue("K♠");
assertEquals(rankTen, 10);

//Test for Invalid Card
try {
const invalidCard = getCardValue("Z♠");
assertEquals(invalidCard, "Invalid card type");
} catch (e) {
console.log(`Invalid card rank was passed. Error: ${e.message}`);
}

// Test for invalid input type
try {
const invalidType = getCardValue(undefined);
assertEquals(invalidType, "Invalid input type");
} catch (e) {
console.log(`Invalid input type was passed. Error: ${e.message}`);
}
20 changes: 8 additions & 12 deletions Sprint-3/2-mandatory-rewrite/1-get-angle-type.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
function getAngleType(angle) {
if (angle === 90) return "Right angle";
// replace with your completed function from key-implement

if (angle < 90 && angle > 0) return "Acute angle";
if (angle > 90 && angle < 180) return "Obtuse angle";
if (angle === 180) return "Straight angle";
if (angle > 180 && angle < 360) return "Reflex angle";
if (angle === 0 || angle === 360) return "Full rotation";
else return "Invalid angle";
}








// Don't get bogged down in this detail
// Jest uses CommonJS module syntax by default as it's quite old
// We will upgrade our approach to ES6 modules in the next course module, so for now
// We will upgrade our approach to ES6 modules in the next course module, so for now
// we have just written the CommonJS module.exports syntax for you
module.exports = getAngleType;
module.exports = getAngleType;
17 changes: 16 additions & 1 deletion Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const getAngleType = require("./1-get-angle-type");

test("should identify right angle (90°)", () => {
test("should identify right angle 90°", () => {
expect(getAngleType(90)).toEqual("Right angle");
});

Expand All @@ -10,15 +10,30 @@ test("should identify right angle (90°)", () => {
// Case 2: Identify Acute Angles:
// When the angle is less than 90 degrees,
// Then the function should return "Acute angle"
test("should identify acute angle less than 90°", () => {
expect(getAngleType(89)).toEqual("Acute angle");
});

// Case 3: Identify Obtuse Angles:
// When the angle is greater than 90 degrees and less than 180 degrees,
// Then the function should return "Obtuse angle"
test("should identify obtuse angle more than 90° but less than 180°", () => {
expect(getAngleType(169)).toEqual("Obtuse angle");
});

// Case 4: Identify Straight Angles:
// When the angle is exactly 180 degrees,
// Then the function should return "Straight angle"
test("should identify straight angle exactly equal to 180°", () => {
expect(getAngleType(180)).toEqual("Straight angle");
});

// Case 5: Identify Reflex Angles:
// When the angle is greater than 180 degrees and less than 360 degrees,
// Then the function should return "Reflex angle"
test("should identify reflex angle is more than 180°", () => {
expect(getAngleType(280)).toEqual("Reflex angle");
});
test("should identify invalid angle value", () => {
expect(getAngleType(-280)).toEqual("Invalid angle");
});
33 changes: 30 additions & 3 deletions Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
function isProperFraction(numerator, denominator) {
if (numerator < denominator) return true;
// add your completed function from key-implement here
if (
typeof numerator !== "number" ||
typeof denominator !== "number" ||
isNaN(numerator) ||
isNaN(denominator)
) {
return false;
}

// Check if denominator is 0 (division by zero is undefined)
if (denominator === 0) {
return false; // Division by zero is not allowed
}

// If numerator is 0, it's a proper fraction (since 0 < any non-zero denominator)
if (numerator === 0) {
return true;
}

// Handle floating-point precision issues: rounding to a specific decimal place
const roundedNumerator = Math.round(numerator * 1e6) / 1e6;
const roundedDenominator = Math.round(denominator * 1e6) / 1e6;

// If numerator is less than denominator, it's a proper fraction
if (Math.abs(roundedNumerator) < Math.abs(roundedDenominator)) {
return true;
}

return false;
}

module.exports = isProperFraction;
module.exports = isProperFraction;
42 changes: 39 additions & 3 deletions Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,44 @@ test("should return true for a proper fraction", () => {
expect(isProperFraction(2, 3)).toEqual(true);
});

// Case 2: Identify Improper Fractions:
test("should return false for a improper fraction", () => {
expect(isProperFraction(10, 4)).toEqual(false);
});

test("should return true for a negative proper fraction", () => {
expect(isProperFraction(-2, -4)).toEqual(true);
});

test("should return false for a fraction where numerator equals denominator", () => {
expect(isProperFraction(4, 4)).toEqual(false);
});

test("should return true for a fraction where the numerator is 0", () => {
expect(isProperFraction(0, 2)).toEqual(true);
});

// Case 3: Identify Negative Fractions:
test("should return false for a fraction where denominator is 0", () => {
expect(isProperFraction(5, 0)).toEqual(false);
});

// Case 4: Identify Equal Numerator and Denominator:
test("should return false for invalid input", () => {
expect(isProperFraction("hello", 2)).toEqual(false);
expect(isProperFraction(null, 2)).toEqual(false);
expect(isProperFraction(2, undefined)).toEqual(false);
expect(isProperFraction(NaN, 2)).toEqual(false);
});

test("should return true for a proper fraction with very large numbers", () => {
expect(isProperFraction(1, Number.MAX_VALUE + 1)).toEqual(true);
expect(isProperFraction(Number.MIN_VALUE - 1, Number.MAX_VALUE + 1)).toEqual(
true
);
});

test("should return false for an improper fraction with very large numbers", () => {
expect(isProperFraction(Number.MAX_VALUE, 1)).toEqual(false);
});

test("should handle floating-point precision correctly", () => {
expect(isProperFraction(0.1 + 0.1, 0.3)).toEqual(true);
});
23 changes: 20 additions & 3 deletions Sprint-3/2-mandatory-rewrite/3-get-card-value.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
function getCardValue(card) {
// replace with your code from key-implement
return 11;
if (typeof card !== "string") throw new Error("Invalid card rank");

const rank = card.slice(0, -1).toUpperCase();

const cardValues = {
A: 11,
K: 10,
Q: 10,
J: 10,
10: 10,
};

if (cardValues[rank] !== undefined) return cardValues[rank];

const numRank = Number(rank);
if (numRank >= 2 && numRank <= 9) return numRank;

throw new Error("Invalid card rank");
}
module.exports = getCardValue;
module.exports = getCardValue;

32 changes: 29 additions & 3 deletions Sprint-3/2-mandatory-rewrite/3-get-card-value.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
const getCardValue = require("./3-get-card-value");

test("should return 11 for Ace of Spades", () => {
const aceofSpades = getCardValue("A♠");
expect(aceofSpades).toEqual(11);
});
const aceofSpades = getCardValue("A♠");
expect(aceofSpades).toEqual(11);
});

// Case 2: Handle Number Cards (2-10):
test("Number cards return correct value", () => {
expect(getCardValue("5♥")).toBe(5);
expect(getCardValue("9♦")).toBe(9);
});

test("Ten card returns 10", () => {
expect(getCardValue("10♣")).toBe(10);
});

// Case 3: Handle Face Cards (J, Q, K):
test("Face cards return 10", () => {
expect(getCardValue("J♠")).toBe(10);
expect(getCardValue("Q♥")).toBe(10);
expect(getCardValue("K♦")).toBe(10);
});

// Case 4: Handle Ace (A):
test("should return 11 for Aces", () => {
expect(getCardValue("A♠")).toEqual(11);
expect(getCardValue("A♥")).toEqual(11);
expect(getCardValue("A♦")).toEqual(11);
});

// Case 5: Handle Invalid Cards:
test("Invalid cards throw error", () => {
expect(() => getCardValue("1♠")).toThrow("Invalid card rank");
expect(() => getCardValue("11♦")).toThrow("Invalid card rank");
expect(() => getCardValue("Z♣")).toThrow("Invalid card rank");
});
Loading