Skip to content

LONDON | ITP_MAY | Mirabel Adom | Module-Structuring-and-Testing-Data | Sprint-3 #665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
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
62 changes: 30 additions & 32 deletions Sprint-3/1-key-implement/1-get-angle-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,47 @@
// 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 (typeof angle !== "number" || angle <= 0 || angle > 360)
return "Invalid angle";
if (angle === 90) return "Right angle";
if (angle === 180) return "Straight angle";
if (angle > 0 && angle < 90) return "Acute angle";
if (angle > 90 && angle < 180) return "Obtuse angle";
if (angle > 180 && angle < 360) return "Reflex angle";

return "Invalid angle";
}

// we're going to use this helper function 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}`
);
}

// Acceptance criteria:

// Given an angle in degrees,
// When the function getAngleType is called with this angle,
// Then it should:

// Case 1: Identify Right Angles:
// When the angle is exactly 90 degrees,
// Then the function should return "Right angle"
const right = getAngleType(90);
assertEquals(right, "Right angle");

// Case 2: Identify Acute Angles:
// When the angle is less than 90 degrees,
// Then the function should return "Acute angle"
const acute = getAngleType(45);
assertEquals(acute, "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"
const obtuse = getAngleType(120);
// ====> write your test here, and then add a line to pass the test in the function above
assertEquals(getAngleType(120), "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"
// ====> write your test here, and then add a line to pass the test in the function above
assertEquals(getAngleType(180), "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"
// ====> write your test here, and then add a line to pass the test in the function above
// ====> write your test here, and then add a line to pass the test in the function above
assertEquals(getAngleType(181), "Reflex angle");
assertEquals(getAngleType(359), "Reflex angle");

// Case 6: No valid input:
// When the angle is greater than 360 degrees and less than 0 degrees,
// Then the function should return "Invalid angle"
// ====> write your test here, and then add a line to pass the test in the function above
assertEquals(getAngleType(-5), "Invalid angle");
assertEquals(getAngleType(361), "Invalid angle");

// Case 6: Not number input:
// When the angle is not a number,
// Then the function should return "Invalid angle"
// ====> write your test here, and then add a line to pass the test in the function above
assertEquals(getAngleType("-5"), "Invalid angle");
assertEquals(getAngleType(true), "Invalid angle");
assertEquals(getAngleType("Hello"), "Invalid angle");
52 changes: 22 additions & 30 deletions 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,16 @@
// 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 denominator !== "number" ||
denominator === 0
) {
return false; // invalid input
}

const value = numerator / denominator;
return value > 0 && value < 1;
}

// here's our helper again
Expand All @@ -19,35 +28,18 @@ function assertEquals(actualOutput, targetOutput) {
);
}

// Acceptance criteria:
// Test 1: Proper Fraction
const test1 = isProperFraction(2, 3);
assertEquals(test1, true);

// Proper Fraction check:
// Input: numerator = 2, denominator = 3
// target output: true
// Explanation: The fraction 2/3 is a proper fraction, where the numerator is less than the denominator. The function should return true.
const properFraction = isProperFraction(2, 3);
assertEquals(properFraction, true);
// Test 2: Improper Fraction
const test2 = isProperFraction(5, 2);
assertEquals(test2, false);

// Improper Fraction check:
// Input: numerator = 5, denominator = 2
// target output: false
// Explanation: The fraction 5/2 is an improper fraction, where the numerator is greater than or equal to the denominator. The function should return false.
const improperFraction = isProperFraction(5, 2);
assertEquals(improperFraction, false);
// Test 3: Negative Proper Fraction (should be false!)
const test3 = isProperFraction(-4, 7);
assertEquals(test3, false);

// Negative Fraction check:
// Input: numerator = -4, denominator = 7
// 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);
// ====> 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);
// ====> complete with your assertion

// Stretch:
// What other scenarios could you test for?
// Test 4: Equal Numerator and Denominator
const test4 = isProperFraction(3, 3);
assertEquals(test4, false);
83 changes: 73 additions & 10 deletions Sprint-3/1-key-implement/3-get-card-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,25 @@
// 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" || card.length < 2) return 0;

// You need to write assertions for your function to check it works in different cases
// we're going to use this helper function 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}`
);
let rank = card;

if (card.startsWith("10")) {
rank = "10";
} else {
rank = card[0];
}

if (rank === "A") return 11;
if (["K", "Q", "J"].includes(rank)) return 10;

const numberValue = Number(rank);
if (!isNaN(numberValue)) return numberValue;

return 0;
}

// Acceptance criteria:

// 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),
Expand Down Expand Up @@ -49,3 +56,59 @@ 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."

function getCardValue(card) {
const rank = card.slice(0, -1); // removes the suit

if (!rank) {
throw new Error("Invalid card format.");
}

if (!isNaN(rank)) {
const num = parseInt(rank);
if (num >= 2 && num <= 10) return num;
}

if (["J", "Q", "K"].includes(rank)) return 10;

if (rank === "A") return 11;

throw new Error("Invalid card rank.");
}

// ✅ Assertion helper
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}

// ✅ Tests
const aceofSpades = getCardValue("A♠");
assertEquals(aceofSpades, 11);

const fiveofHearts = getCardValue("5♥");
assertEquals(fiveofHearts, 5);

const tenofClubs = getCardValue("10♣");
assertEquals(tenofClubs, 10);

const jackofDiamonds = getCardValue("J♦");
assertEquals(jackofDiamonds, 10);

const queenofSpades = getCardValue("Q♠");
assertEquals(queenofSpades, 10);

const kingofHearts = getCardValue("K♥");
assertEquals(kingofHearts, 10);

// Error handling test
try {
getCardValue("Z♦");
} catch (error) {
console.assert(
error.message === "Invalid card rank.",
`Expected error message but got "${error.message}"`
);
}
18 changes: 7 additions & 11 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 === 180) return "Straight angle";
if (angle > 0 && angle < 90) return "Acute angle";
if (angle > 90 && angle < 180) return "Obtuse angle";
if (angle > 180 && angle < 360) return "Reflex angle";

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;
20 changes: 12 additions & 8 deletions Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you implementation pass the tests in this script?

Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ test("should identify right angle (90°)", () => {
// make your test descriptions as clear and readable as possible

// 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 (e.g., 45°)", () => {
expect(getAngleType(45)).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 (e.g., 120°)", () => {
expect(getAngleType(120)).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 (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 (e.g., 181°)", () => {
expect(getAngleType(181)).toEqual("Reflex angle");
});
Comment on lines +26 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also prepare the test like this:

test("should identify reflex angle (180 < angle < 360)", () => {
  expect(getAngleType(300)).toEqual("Reflex angle");
  expect(getAngleType(359.999)).toEqual("Reflex angle");
  expect(getAngleType(181)).toEqual("Reflex angle");
});
  • No need to specify the value being tested in the test description.
  • We can check different samples within a test.

22 changes: 19 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,22 @@
function isProperFraction(numerator, denominator) {
if (numerator < denominator) return true;
// add your completed function from key-implement here
if (
typeof numerator !== "number" ||
typeof denominator !== "number" ||
denominator === 0
) {
return false; // invalid input
}

const value = numerator / denominator;
return value > 0 && value < 1;
}
Comment on lines +2 to +12
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In mathematics, -A/B == A/-B == -(A/B), and -A/-B == A/B for any integers A and B (B ≠ 0).
They represent a proper fraction if A < B and A ≠ 0 and B ≠ 0.

So isProperFraction(-4, 3) should return false because 4 >= 3.

So isProperFraction(-2, 5) should return true because 2 < 5.

Consider comparing the absolute value of the numerator and the denominator instead.


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

module.exports = isProperFraction;
module.exports = isProperFraction;
10 changes: 9 additions & 1 deletion Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ test("should return true for a proper fraction", () => {
});

// Case 2: Identify Improper Fractions:

test("should return false for an improper fraction (numerator > denominator)", () => {
expect(isProperFraction(5, 2)).toEqual(false);
});
// Case 3: Identify Negative Fractions:
test("should return true for a proper fraction with both numerator and denominator negative", () => {
expect(isProperFraction(-4, -7)).toEqual(true);
});

// Case 4: Identify Equal Numerator and Denominator:
test("should return false when numerator equals denominator", () => {
expect(isProperFraction(3, 3)).toEqual(false);
});
21 changes: 18 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,20 @@
function getCardValue(card) {
// replace with your code from key-implement
return 11;
if (typeof card !== "string" || card.length < 2) return 0;

let rank = card;

if (card.startsWith("10")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach will also accept values like "100000♣" or "10ABC♣".

Consider removing the suit character from card and then check the remaining characters.

rank = "10";
} else {
rank = card[0];
}

if (rank === "A") return 11;
if (["K", "Q", "J"].includes(rank)) return 10;

const numberValue = Number(rank);
if (!isNaN(numberValue)) return numberValue;

return 0;
}
module.exports = getCardValue;
module.exports = getCardValue;
Loading