Skip to content

WESTMIDLANDS | ITP-MAY-25 | AHMAD EHSAS | MODULE STRUCTURING AND TESTING DATA SPRINT-3 #656

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 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 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
2 changes: 2 additions & 0 deletions Sprint-1/1-key-exercises/1-count.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ count = count + 1;

// Line 1 is a variable declaration, creating the count variable with an initial value of 0
// Describe what line 3 is doing, in particular focus on what = is doing
// line 3 increases the value of count by 1.
// the "=" operator assigns the result of "count + 1" back to the variable count.
4 changes: 2 additions & 2 deletions Sprint-1/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This README will guide you through the different sections for this week.

## 1 Exercises

In this section, you'll have a short program and task. Some of the syntax may be unfamiliar - in this case, you'll need to look things up in documentation.
In this section, you'll have a short program and task. Some of the syntax may be unfamiliar - in this case, you'll need to look things up in documentation.

https://developer.mozilla.org/en-US/docs/Web/JavaScript

Expand All @@ -28,7 +28,7 @@ You must use documentation to make sense of anything unfamiliar - learning how t

You can also use `console.log` to check the value of different variables in the code.

https://developer.mozilla.org/en-US/docs/Web/JavaScript
https://developer.mozilla.org/en-US/docs/Web/JavaScript

## 4 Explore - Stretch 💪

Expand Down
16 changes: 13 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";
if (angle < 90) 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";

// read to the end, complete line 36, then pass your test here
}

// we're going to use this helper function to make our assertions easier to read
Expand Down Expand Up @@ -43,14 +48,19 @@ 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"
// ====> write your test here, and then add a line to pass the test in the function above
const straight = getAngleType(180);
assertEquals(straight, "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
const reflex = getAngleType(270);
assertEquals(reflex, "Reflex angle");
7 changes: 5 additions & 2 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,9 @@
// 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 (Math.abs(numerator) < denominator) return true; // This version of code works correctly for proper and negative fractions.
if (Math.abs(numerator) >= Math.abs(denominator)) return false;
if (Math.abs(numerator) === Math.abs(denominator)) return false;
}

// here's our helper again
Expand Down Expand Up @@ -40,13 +42,14 @@ 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);
// ====> complete with your assertion
assertEquals(negativeFraction, true); // assertion for negative fraction.

// 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); // assertion for equal numerator and denominator.
// ====> complete with your assertion

// Stretch:
Expand Down
34 changes: 32 additions & 2 deletions Sprint-3/1-key-implement/3-get-card-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,24 @@
// 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;
const rank = card.slice(0, -1); // get the rank (before the suit symbol)

if (!isNaN(rank)) {
return Number(rank); // Number card
}

if (rank === "J" || rank === "Q" || rank === "K") {
return 10; // Face cards
}

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

// Anything else is invalid
throw new Error("Invalid card rank");
}

// if the rank is not a number or a face card, throw an error(invalid card rank).

// 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
Expand All @@ -33,13 +48,23 @@ assertEquals(aceofSpades, 11);
// 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
assertEquals(fiveofHearts, 5);

const tenofDiamonds = getCardValue("10♦");
assertEquals(tenofDiamonds, 10);

// 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.
const jackofClubs = getCardValue("J♣");
assertEquals(jackofClubs, 10);

const queenofHearts = getCardValue("Q♥");
assertEquals(queenofHearts, 10);

const kingofSpades = getCardValue("K♠");
assertEquals(kingofSpades, 10);
// Handle Ace (A):
// Given a card with a rank of "A",
// When the function is called with an Ace,
Expand All @@ -49,3 +74,8 @@ 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."
try {
getCardValue("Z♠");
} catch (error) {
console.log("Caught error:", error.message); // Should say "Invalid card rank"
}
17 changes: 7 additions & 10 deletions Sprint-3/2-mandatory-rewrite/1-get-angle-type.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
function getAngleType(angle) {
if (angle === 90) return "Right angle";
// replace with your completed function from key-implement
if (angle < 90) 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";

return "Invalid angle";
Copy link
Contributor

Choose a reason for hiding this comment

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

Technically angles less than or equal to 0 are also considered "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;
29 changes: 16 additions & 13 deletions Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ test("should identify right angle (90°)", () => {

// REPLACE the comments with the tests
// make your test descriptions as clear and readable as possible
test("should identify acute angle (< 90°)", () => {
expect(getAngleType(45)).toEqual("Acute angle");
});
// Case 2: identify Acute Angles, When the angle is less than 90 degrees, than the function should return "Acute angle".

// Case 2: Identify Acute Angles:
// When the angle is less than 90 degrees,
// Then the function should return "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 (> 90° and < 180°)", () => {
expect(getAngleType(120)).toEqual("Obtuse angle");
});
// Case 3: identify Obtuse Angles, When the angle is greater than 90 degrees and less than 180 degrees, than the function should return "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 4: identify Straight Angles, When the angle is exactly 180 degrees, than the function should return "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 (> 180° and < 360°)", () => {
expect(getAngleType(270)).toEqual("Reflex angle");
});
// Case 5: identify Reflex Angles, When the angle is greater than 180 degrees and less than 360 degrees, the function should return "Reflex Angle".
7 changes: 4 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,7 @@
function isProperFraction(numerator, denominator) {
if (numerator < denominator) return true;
// add your completed function from key-implement here
if (Math.abs(numerator) < Math.abs(denominator)) return true; // This version of code works correctly for proper and negative fractions.
if (Math.abs(numerator) >= Math.abs(denominator)) return false;
if (Math.abs(numerator) === Math.abs(denominator)) return false;
}
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 use else to simplify your code?


module.exports = isProperFraction;
module.exports = isProperFraction;
12 changes: 12 additions & 0 deletions 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,19 @@ test("should return true for a proper fraction", () => {
});

// Case 2: Identify Improper Fractions:
test("should return false for improper fractions", () => {
const improperFraction = isProperFraction(5, 2);
expect(improperFraction).toEqual(false);
});

// Case 3: Identify Negative Fractions:
test("should return true for negative fractions", () => {
const negativeFraction = isProperFraction(-4, 7);
expect(negativeFraction).toEqual(true);
});

// Case 4: Identify Equal Numerator and Denominator:
test("should return false for equal numerator and denominator", () => {
const equalFraction = isProperFraction(3, 3);
expect(equalFraction).toEqual(false);
});
14 changes: 11 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,13 @@
function getCardValue(card) {
// replace with your code from key-implement
return 11;
const rank = card.slice(0, -1); // get the value part: A, 2–10, J, Q, K

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

const number = parseInt(rank);
Copy link
Contributor

Choose a reason for hiding this comment

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

Does your function return the value you expected from each of the following function calls?

getCardValue("0x02♠");
getCardValue("2.1♠");
getCardValue("0002♠");
getCardValue("3ABC♠");

Consider looking up how parseInt() work.

if (!isNaN(number) && number >= 2 && number <= 10) return number;

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

module.exports = getCardValue;
37 changes: 34 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,9 +1,40 @@
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);
});

test("should return 5 for 5 of Hearts", () => {
const fiveofHearts = getCardValue("5♥");
expect(fiveofHearts).toEqual(5);
});

test("should return 10 for 10 of Diamonds", () => {
const tenofDiamonds = getCardValue("10♦");
expect(tenofDiamonds).toEqual(10);
});
Comment on lines +8 to +16
Copy link
Contributor

Choose a reason for hiding this comment

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

When preparing tests, we should ensure the tests cover all possible cases. If we specify a test for individual card, we will need about 53 tests to cover all possible cases. Instead, we could consider classifying all possible values into different categories, and then within each category we test some samples.

For example, one possible category for getCardValue() is, "should return the value of number cards (2-10)", and we can prepare the test as

test("should return the value of number cards (2-10)", () => {
    expect(getCardValue("2♣︎")).toEqual(2);
    expect(getCardValue("5♠")).toEqual(5);
    expect(getCardValue("10♥")).toEqual(10);
    // Note: We could also use a loop to check all values from 2 to 10.
});


test("should return 10 for Jack of Clubs", () => {
const jackofClubs = getCardValue("J♣");
expect(jackofClubs).toEqual(10);
});

test("should return 10 for Queen of Hearts", () => {
const queenofHearts = getCardValue("Q♥");
expect(queenofHearts).toEqual(10);
});

test("should return 10 for King of Spades", () => {
const kingofSpades = getCardValue("K♠");
expect(kingofSpades).toEqual(10);
});
Comment on lines +18 to +31
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 combine these tests and place them under the category "should return 10 for face cards (J, Q, K)".


try {
getCardValue("Z♠");
} catch (error) {
console.log("Caught error:", error.message);
}
Comment on lines +33 to +37
Copy link
Contributor

Choose a reason for hiding this comment

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

Jest offers a method called toThrow() that can be used to check if a function can throw the expected error.

You can find out more about how to use .toThrow() here: https://jestjs.io/docs/expect#tothrowerror (Note: Pay close attention to the syntax of the example)


// Case 2: Handle Number Cards (2-10):
// Case 3: Handle Face Cards (J, Q, K):
Expand Down
14 changes: 12 additions & 2 deletions Sprint-3/3-mandatory-practice/implement/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
let count = 0; // start a count with 0
for (let char of stringOfCharacters) {
// repeat for each character in the string
if (char === findCharacter) {
// check if the character matches the one we are looking for.
count++; // if it does increase the count by 1.
}
}

return count;
}
console.log(countChar("aAaAaAaAa", "a")); // example usage should return 5.

module.exports = countChar;
module.exports = countChar;
9 changes: 9 additions & 0 deletions Sprint-3/3-mandatory-practice/implement/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,12 @@ test("should count multiple occurrences of a character", () => {
// And a character char that does not exist within the case-sensitive str,
// When the function is called with these inputs,
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.

test("should return 0 when character does not exist in the string", () => {
const str = "aaaaa";
const char = "b";
const result = countChar(str, char);
expect(result).toEqual(0);
});
// the above test cases are for the countChar function that counts the number of times a character occurs in a string.
// this function should return 0 because the character `b` does not exist in the string "aaaaa".
10 changes: 8 additions & 2 deletions Sprint-3/3-mandatory-practice/implement/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function getOrdinalNumber(num) {
return "1st";
if (num === 1) {
return "1st";
}
Comment on lines +2 to +4
Copy link
Contributor

Choose a reason for hiding this comment

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

This function is not fully implemented; it should work for any valid positive integers.

Consider looking up the rules to clarify how ordinal numbers are formed.

}
console.log(getOrdinalNumber(1)); // The output should be "1st"

module.exports = getOrdinalNumber;
// in this function, we got the ordinal number for 1.
// we used a simple if statement to check if the number is 1 and return "1st".

module.exports = getOrdinalNumber;
Copy link
Contributor

Choose a reason for hiding this comment

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

The tests in this Jest script are incomplete. When you have fully implemented the function in Sprint-3/3-mandatory-practice/implement/get-ordinal-number.js, can you prepare tests in this script to cover all valid integers in the following way:

Group all possible input values into meaningful categories, and then, select representative samples from each category to test. This approach improves coverage and makes our tests easier to maintain.

For example, we can prepare a test for numbers 2, 22, 132, etc. as

test("append 'nd' to numbers ending in 2, except those ending in 12", () => {
    expect( getOrdinalNumber(2) ).toEqual("2nd");
    expect( getOrdinalNumber(22) ).toEqual("22nd");
    expect( getOrdinalNumber(132) ).toEqual("132nd");
});

Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ const getOrdinalNumber = require("./get-ordinal-number");
// Then the function should return "1st"

test("should return '1st' for 1", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
});
expect(getOrdinalNumber(1)).toEqual("1st");
});
7 changes: 4 additions & 3 deletions Sprint-3/3-mandatory-practice/implement/repeat.js
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not update the tests in Sprint-3/3-mandatory-practice/implement/repeat.test.js to check your implementation?

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function repeat() {
return "hellohellohello";
function repeat(word, times) {
return word.repeat(times);
}
console.log(repeat("hello", 3)); // The output should be "hellohellohello"

module.exports = repeat;
module.exports = repeat;
Loading