Skip to content

LONDON| MAY 2025 | HAKAN MURAT KAVUT | Module-Structuring-and-Testing-Data - Sprint 3 #611

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 19 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
14 changes: 14 additions & 0 deletions Sprint-1/3-mandatory-interpret/aaa.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
for (let a = 1; a <= 9; a++) {
for (let b = 0; b <= 9; b++) {
for (let c = 1; c <= 9; c++) {
for (let d = 0; d <= 9; d++) {
let left = Math.pow(a, b) * Math.pow(c, d);
let right = parseInt(`${a}${b}${c}${d}`);

if (left === right) {
console.log(`Match found: (${a}^${b}) * (${c}^${d}) = ${left}`);
}
}
}
}
}
16 changes: 16 additions & 0 deletions Sprint-1/3-mandatory-interpret/codewars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function transposeTwoStrings(array) {
let a = "";
let no = Math.max(array[0].length, array[1].length);
for (let i = 0; i < no; i++) {
if (i < array[0].length) {
a += array[0][i];
} else a += " ";
a += " ";
if (i < array[1].length) {
a += array[1][i];
} else a += " ";
if (i < array[1].length - 1 || i < array[0].length - 1) a += "\n";
}

return a;
}
17 changes: 14 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,12 @@
// 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 === 45) return "Acute angle";
if (angle === 120) 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 +47,21 @@ 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(181);
assertEquals(reflex, "Reflex angle");
const reflex2 = getAngleType(359);
assertEquals(reflex2, "Reflex angle");
21 changes: 20 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,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) < Math.abs(denominator)) return true;
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 @@ -41,13 +43,30 @@ assertEquals(improperFraction, false);
// 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);

// 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
assertEquals(equalFraction, false);

// Stretch:
// What other scenarios could you test for?

// Equal Numerator and Denominator check:
// Input: numerator = 0, denominator = 5
// target output: true
// Explanation: The fraction 0/5 is a proper fraction, where the numerator is less than the denominator. The function should return true.
const properFraction2 = isProperFraction(0, 10);
assertEquals(properFraction2, true);

// Negative Fraction check:
// Input: numerator = 3, denominator = -6
// target output: true
// Explanation: The fraction 3/-5 is a negative fraction because the absolute value of the numerator (3) is less than the denominator (5). The function should return true.
const negativeFraction2 = isProperFraction(3, -6);
// ====> complete with your assertion
assertEquals(negativeFraction2, true);
13 changes: 12 additions & 1 deletion 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,11 @@
// 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;
let rank = card[0];
if (rank === "A") return 11;
if (rank === "J" || rank === "Q" || rank === "K") return 10;
if (Number(card[0]) > 0 && Number(card[0]) < 11) return Number(card[0]);
return "Invalid card rank.";
}

// You need to write assertions for your function to check it works in different cases
Expand All @@ -33,19 +37,26 @@ 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♥");
assertEquals(fiveofHearts, 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.
const faceofHearts = getCardValue("J♥");
assertEquals(faceofHearts, 10);

// Handle Ace (A):
// Given a card with a rank of "A",
// When the function is called with an Ace,
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
const aceofHearts = getCardValue("A♥");
assertEquals(aceofHearts, 11);

// Handle Invalid Cards:
// 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."
const invalidCard = getCardValue("C♥");
assertEquals(invalidCard, "Invalid card rank.");
17 changes: 6 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,13 @@
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";
Comment on lines +3 to +6
Copy link
Contributor

Choose a reason for hiding this comment

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

The spec isn't clear whether angle can be assigned a number not in the interval (0, 360).
When angle is >= 360, what should the function return? (Also, by definition, angles <= 0 are not considered acute angles.)

When we implement a function that can return a value, to ensure reliability, we should ensure it will always return a defined value instead of undefined (which represents "no return value").
If the parameter, angle, is not within the recognised range, we can design the function to return a special value (e.g., "Invalid angle") or throw an error.

}








// 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;
12 changes: 12 additions & 0 deletions Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,27 @@ 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 right angle (75°)", () => {
expect(getAngleType(75)).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 (x>90°)", () => {
expect(getAngleType(150)).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 (180°<x<360°)", () => {
expect(getAngleType(290)).toEqual("Reflex angle");
});
5 changes: 3 additions & 2 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;
if (Math.abs(numerator) > Math.abs(denominator)) return true;
if (Math.abs(numerator) === Math.abs(denominator)) return true;
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.

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.

The function is supposed to return true only when numerator/denominator represent a proper fraction.

}

module.exports = isProperFraction;
9 changes: 9 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,16 @@ test("should return true for a proper fraction", () => {
});

// Case 2: Identify Improper Fractions:
test("should return true for a improper fraction", () => {
expect(isProperFraction(5, 2)).toEqual(true);
Copy link
Contributor

Choose a reason for hiding this comment

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

"Identify improper fractions" means, "should return false for a improper fraction"

});

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

// Case 4: Identify Equal Numerator and Denominator:
test("should return true for a equal fraction", () => {
expect(isProperFraction(3, 3)).toEqual(true);
});
Comment on lines +18 to +20
Copy link
Contributor

Choose a reason for hiding this comment

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

3/3 is an improper fraction, so the function is expected to return false.

9 changes: 6 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,8 @@
function getCardValue(card) {
// replace with your code from key-implement
return 11;
let rank = card[0];
Copy link
Contributor

Choose a reason for hiding this comment

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

card[0] only refers to the first character.

if (rank === "A") return 11;
if (rank === "J" || rank === "Q" || rank === "K") return 10;
if (Number(card[0]) > 0 && Number(card[0]) < 11) return Number(card[0]);
return "Invalid card rank.";
}
module.exports = getCardValue;
module.exports = getCardValue;
22 changes: 19 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,27 @@
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("should return number for card number bw 2-10", () => {
const fiveofHearts = getCardValue("5♥");
expect(fiveofHearts).toEqual(5);
});
// Case 3: Handle Face Cards (J, Q, K):
test("should return number for face of cards", () => {
const faceofHearts = getCardValue("J♥");
expect(faceofHearts).toEqual(10);
});
// Case 4: Handle Ace (A):
test("should return number for face of cards", () => {
const aceofHearts = getCardValue("A♥");
expect(aceofHearts).toEqual(11);
});
// Case 5: Handle Invalid Cards:
test("should return number for face of cards", () => {
const invalidCard = getCardValue("C♥");
expect(invalidCard).toEqual("Invalid card rank.");
});
Comment on lines +24 to +27
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("111111♠");
getCardValue("2.1♠");
getCardValue("2AAAAA♠");
getCardValue("10♠");

13 changes: 12 additions & 1 deletion Sprint-3/3-mandatory-practice/implement/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
if (!stringOfCharacters.includes(findCharacter)) {
return "No occurrence";
}
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.

When a function can return different types of value, it will make consuming the return value inconvenience.

For example, let str1 and str2 be two variables storing some string values.
How can we use countChar() to check if str1 contains more space characters than str2?

Can you think of a better value to return when stringOfCharacters does not contain any instance of findCharacter?


let count = 0;
for (let char of stringOfCharacters) {
if (char === findCharacter) {
count++;
}
}
return count;
}


module.exports = countChar;
6 changes: 6 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,9 @@ 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 there is no occurrence", () => {
const str = "aaaaa";
const char = "b";
const count = countChar(str, char);
expect(count).toEqual("No occurrence");
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function getOrdinalNumber(num) {
return "1st";
if (num % 10 ==1 && num % 100 !=11 )
return "1st";
if (num % 10 ==2 && num % 100 !=12 )
return "2nd";
if (num % 10 ==3 && num % 100 !=13 )
return "3rd";
return "th"
}

module.exports = getOrdinalNumber;
18 changes: 18 additions & 0 deletions Sprint-3/3-mandatory-practice/implement/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,21 @@ const getOrdinalNumber = require("./get-ordinal-number");
test("should return '1st' for 1", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
});
test("should return '2nd' for 2", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
});
Comment on lines +14 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.

To ensure thorough testing, we need broad scenario coverage. Listing individual values, however, can quickly lead to an unmanageable number of test cases.
Instead of writing tests for individual numbers, consider grouping all possible input values into meaningful categories. 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");
});

You may also discover a bug in your implementation when you test your function using more samples.

test("should return '3rd' for 3", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
});
test("should return 'th' for 11", () => {
expect(getOrdinalNumber(11)).toEqual("th");
});
test("should return 'th' for 12", () => {
expect(getOrdinalNumber(12)).toEqual("th");
});
test("should return 'th' for 13", () => {
expect(getOrdinalNumber(13)).toEqual("th");
Comment on lines +20 to +27
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 specify these tests under the category, "should append 'th' for numbers ending in 11, 12, 13".

});
test("should return 'th' for 4", () => {
expect(getOrdinalNumber(4)).toEqual("th");
Comment on lines +29 to +30
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 generalise this test to, "should append 'th' for numbers ending in 0, 4-9".

});
16 changes: 13 additions & 3 deletions Sprint-3/3-mandatory-practice/implement/repeat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
function repeat() {
return "hellohellohello";
function repeat(str, count) {

if (Number(count) > 1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you explicitly convert count to a number? Isn't count already a number?

let result=""
for (let i = 0; i <= count-1; i++) {
result += str;
}
return result;
}
if (count == 1) return str;
if (count == 0) return "";
return "Negative counts are not valid.";
Copy link
Contributor

Choose a reason for hiding this comment

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

How would the caller distinguish the result of the following two function calls?

  1. repeat("Negative counts are not valid.", 1)
  2. repeat("", -1)

Both function calls return the same value.

}

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