Skip to content

Commit 9c4a284

Browse files
committed
Glasgow | Jan-26 | Fattouma Ouannassi | Sprint 3 | Coursework practice tdd
<!-- You must title your PR like this: Region | Cohort | FirstName LastName | Sprint | Assignment Title For example, London | 25-ITP-May | Carol Owen | Sprint 1 | Alarm Clock Fill in the template below - remove any sections that don't apply. Complete the self checklist - replace each empty box in the checklist [ ] with a [x]. Add the label "Needs Review" and you will get review. Respond to volunteer reviews until the volunteer marks it as "Complete". Please note: if the PR template is not filled as described above, an automatic GitHub bot will give feedback in the "Conversation" tab of the pull request and not allow the "Needs Review" label to be added until it's fixed. --> ## Learners, PR Template Self checklist - [x] I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title - [x] My changes meet the requirements of the task - [x] I have tested my changes - [x] My changes follow the [style guide](https://curriculum.codeyourfuture.io/guides/reviewing/style-guide/) ## Changelist This project (sprint 3) focuses on learning how to use JavaScript methods and test the code. ## Questions /
1 parent 124ae45 commit 9c4a284

File tree

6 files changed

+218
-37
lines changed

6 files changed

+218
-37
lines changed

Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,52 @@
1010
// After you have implemented the function, write tests to cover all the cases, and
1111
// execute the code to ensure all tests pass.
1212

13-
function isProperFraction(numerator, denominator) {
13+
//function isProperFraction(numerator, denominator) {
1414
// TODO: Implement this function
15-
}
15+
//}
1616

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

2121
// Here's our helper again
22-
function assertEquals(actualOutput, targetOutput) {
22+
/*function assertEquals(actualOutput, targetOutput) {
2323
console.assert(
2424
actualOutput === targetOutput,
2525
`Expected ${actualOutput} to equal ${targetOutput}`
26-
);
27-
}
26+
);*/
27+
//}
2828

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

3232
// Example: 1/2 is a proper fraction
33+
//assertEquals(isProperFraction(1, 2), true);
34+
function isProperFraction(numerator, denominator) {
35+
// Return non numeric numerators and denominators as false
36+
if (typeof numerator != "number" || typeof denominator != "number")
37+
return false;
38+
39+
return Math.abs(numerator) < Math.abs(denominator);
40+
}
41+
42+
module.exports = isProperFraction;
43+
44+
function assertEquals(actualOutput, targetOutput) {
45+
console.assert(
46+
actualOutput === targetOutput,
47+
`Expected ${actualOutput} to equal ${targetOutput}`
48+
);
49+
}
50+
51+
// Positive proper fraction test
3352
assertEquals(isProperFraction(1, 2), true);
53+
54+
// Improper proper fraction test
55+
assertEquals(isProperFraction(3, 2), false);
56+
57+
// Negative proper fraction test
58+
assertEquals(isProperFraction(-1, 2), true);
59+
60+
// Zero numerator test
61+
assertEquals(isProperFraction(0, 2), true);

Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,77 @@
2121
// After you have implemented the function, write tests to cover all the cases, and
2222
// execute the code to ensure all tests pass.
2323

24-
function getCardValue(card) {
24+
/*function getCardValue(card) {
2525
// TODO: Implement this function
26-
}
26+
}*/
2727

2828
// The line below allows us to load the getCardValue function into tests in other files.
2929
// This will be useful in the "rewrite tests with jest" step.
30-
module.exports = getCardValue;
30+
//module.exports = getCardValue;
3131

3232
// Helper functions to make our assertions easier to read.
33-
function assertEquals(actualOutput, targetOutput) {
33+
/*function assertEquals(actualOutput, targetOutput) {
3434
console.assert(
3535
actualOutput === targetOutput,
3636
`Expected ${actualOutput} to equal ${targetOutput}`
3737
);
38-
}
38+
}*/
3939

4040
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
41-
// Examples:
42-
assertEquals(getCardValue("9♠"), 9);
41+
// Examples:function getCardValue(card) {
42+
// Ensure that the last char is a suit, otherwise throw an error
43+
function getCardValue(card) {
44+
// Type check
45+
if (typeof card !== "string") {
46+
throw new Error("Invalid card");
47+
}
48+
49+
const suits = ["♠", "♥", "♦", "♣"];
50+
if (!suits.includes(card.slice(-1))) {
51+
throw new Error("Invalid card");
52+
}
53+
54+
const rank = card.slice(0, -1);
55+
const validRank = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
56+
57+
if (!validRank.includes(rank)) {
58+
throw new Error("Invalid card");
59+
}
60+
61+
// Clean lookup for values
62+
const rankValues = {
63+
"A": 11, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9, "10": 10,
64+
"J": 10, "Q": 10, "K": 10
65+
};
66+
67+
return rankValues[rank];
68+
}
69+
70+
module.exports = getCardValue;
71+
72+
73+
function assertEquals(actualOutput, targetOutput) {
74+
console.assert(
75+
actualOutput === targetOutput,
76+
`Expected ${actualOutput} to equal ${targetOutput}`
77+
);
78+
}
4379

44-
// Handling invalid cards
45-
try {
46-
getCardValue("invalid");
80+
// Tests
81+
assertEquals(getCardValue("A♠"), 11);
82+
assertEquals(getCardValue("K♥"), 10);
83+
assertEquals(getCardValue("10♦"), 10);
84+
assertEquals(getCardValue("2♣"), 2);
4785

48-
// This line will not be reached if an error is thrown as expected
49-
console.error("Error was not thrown for invalid card");
50-
} catch (e) {}
86+
// Invalid card tests
87+
const invalidCards = ["invalid", 7, "", "AA♠", "10♠♦", "10"];
88+
invalidCards.forEach(invalidCard => {
89+
try {
90+
getCardValue(invalidCard);
91+
console.error(`❌ Error not thrown for: ${invalidCard}`);
92+
} catch (e) {
93+
// Expected
94+
}
95+
});
5196

52-
// What other invalid card cases can you think of?
97+
console.log("\n✅ All tests completed!");
Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,43 @@
1-
// This statement loads the getAngleType function you wrote in the implement directory.
2-
// We will use the same function, but write tests for it using Jest in this file.
31
const getAngleType = require("../implement/1-get-angle-type");
42

5-
// TODO: Write tests in Jest syntax to cover all cases/outcomes,
6-
// including boundary and invalid cases.
7-
83
// Case 1: Acute angles
94
test(`should return "Acute angle" when (0 < angle < 90)`, () => {
10-
// Test various acute angles, including boundary cases
115
expect(getAngleType(1)).toEqual("Acute angle");
126
expect(getAngleType(45)).toEqual("Acute angle");
137
expect(getAngleType(89)).toEqual("Acute angle");
148
});
159

1610
// Case 2: Right angle
11+
test(`should return "Right angle" when (angle === 90)`, () => {
12+
expect(getAngleType(90)).toEqual("Right angle");
13+
});
14+
1715
// Case 3: Obtuse angles
16+
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
17+
expect(getAngleType(91.0)).toEqual("Obtuse angle");
18+
expect(getAngleType(100)).toEqual("Obtuse angle");
19+
expect(getAngleType(179)).toEqual("Obtuse angle");
20+
});
21+
1822
// Case 4: Straight angle
23+
test(`should return "Straight angle" when (angle === 180)`, () => {
24+
expect(getAngleType(180)).toEqual("Straight angle");
25+
});
26+
1927
// Case 5: Reflex angles
28+
test(`should return "Reflex angle" when (180 < angle < 360)`, () => {
29+
expect(getAngleType(181)).toEqual("Reflex angle");
30+
expect(getAngleType(200)).toEqual("Reflex angle");
31+
expect(getAngleType(359)).toEqual("Reflex angle");
32+
});
33+
2034
// Case 6: Invalid angles
35+
test(`should return "Invalid angle" when angle doesn't lie between 0-360 exclusive`, () => {
36+
expect(getAngleType(0)).toEqual("Invalid angle");
37+
expect(getAngleType(360)).toEqual("Invalid angle");
38+
expect(getAngleType(500)).toEqual("Invalid angle");
39+
expect(getAngleType(-200)).toEqual("Invalid angle");
40+
expect(getAngleType("")).toEqual("Invalid angle");
41+
expect(getAngleType("ten degrees")).toEqual("Invalid angle");
42+
expect(getAngleType(true)).toEqual("Invalid angle");
43+
});
Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,51 @@
11
// This statement loads the isProperFraction function you wrote in the implement directory.
22
// We will use the same function, but write tests for it using Jest in this file.
3-
const isProperFraction = require("../implement/2-is-proper-fraction");
3+
//const isProperFraction = require("../implement/2-is-proper-fraction");
44

55
// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.
66

7-
// Special case: numerator is zero
8-
test(`should return false when denominator is zero`, () => {
9-
expect(isProperFraction(1, 0)).toEqual(false);
7+
8+
const getAngleType = require("../implement/1-get-angle-type");
9+
10+
// Case 1: Acute angles
11+
test(`should return "Acute angle" when (0 < angle < 90)`, () => {
12+
expect(getAngleType(1)).toEqual("Acute angle");
13+
expect(getAngleType(45)).toEqual("Acute angle");
14+
expect(getAngleType(89)).toEqual("Acute angle");
15+
});
16+
17+
// Case 2: Right angle
18+
test(`should return "Right angle" when (angle === 90)`, () => {
19+
expect(getAngleType(90)).toEqual("Right angle");
20+
});
21+
22+
// Case 3: Obtuse angles
23+
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
24+
expect(getAngleType(91.0)).toEqual("Obtuse angle");
25+
expect(getAngleType(100)).toEqual("Obtuse angle");
26+
expect(getAngleType(179)).toEqual("Obtuse angle");
27+
});
28+
29+
// Case 4: Straight angle
30+
test(`should return "Straight angle" when (angle === 180)`, () => {
31+
expect(getAngleType(180)).toEqual("Straight angle");
32+
});
33+
34+
// Case 5: Reflex angles
35+
test(`should return "Reflex angle" when (180 < angle < 360)`, () => {
36+
expect(getAngleType(181)).toEqual("Reflex angle");
37+
expect(getAngleType(200)).toEqual("Reflex angle");
38+
expect(getAngleType(359)).toEqual("Reflex angle");
39+
});
40+
41+
// Case 6: Invalid angles
42+
test(`should return "Invalid angle" when angle doesn't lie between 0-360 exclusive`, () => {
43+
expect(getAngleType(0)).toEqual("Invalid angle");
44+
expect(getAngleType(360)).toEqual("Invalid angle");
45+
expect(getAngleType(500)).toEqual("Invalid angle");
46+
expect(getAngleType(-200)).toEqual("Invalid angle");
47+
expect(getAngleType("")).toEqual("Invalid angle");
48+
expect(getAngleType("ten degrees")).toEqual("Invalid angle");
49+
expect(getAngleType(true)).toEqual("Invalid angle");
1050
});
51+
// ... rest of isProperFraction tests
Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// This statement loads the getCardValue function you wrote in the implement directory.
22
// We will use the same function, but write tests for it using Jest in this file.
3-
const getCardValue = require("../implement/3-get-card-value");
3+
//const getCardValue = require("../implement/3-get-card-value");
44

55
// TODO: Write tests in Jest syntax to cover all possible outcomes.
66

77
// Case 1: Ace (A)
8-
test(`Should return 11 when given an ace card`, () => {
9-
expect(getCardValue("A♠")).toEqual(11);
10-
});
8+
//test(`Should return 11 when given an ace card`, () => {
9+
//expect(getCardValue("A♠")).toEqual(11);});
1110

1211
// Suggestion: Group the remaining test data into these categories:
1312
// Number Cards (2-10)
@@ -17,4 +16,45 @@ test(`Should return 11 when given an ace card`, () => {
1716
// To learn how to test whether a function throws an error as expected in Jest,
1817
// please refer to the Jest documentation:
1918
// https://jestjs.io/docs/expect#tothrowerror
19+
const getCardValue = require("../implement/3-get-card-value");
20+
const suits = ["♠", "♥", "♦", "♣"];
21+
22+
// Case 1: Ace (A)
23+
test(`Should return 11 when given an ace card`, () => {
24+
for (const suit of suits) {
25+
const ace = `A${suit}`;
26+
expect(getCardValue(ace)).toEqual(11);
27+
}
28+
});
29+
30+
// Case 2: Face Cards (J, Q, K)
31+
test(`Should return 10 when given a face card`, () => {
32+
const faceCards = ["J", "Q", "K"];
33+
for (const faceCard of faceCards) {
34+
for (const suit of suits) {
35+
expect(getCardValue(`${faceCard}${suit}`)).toEqual(10);
36+
}
37+
}
38+
});
39+
40+
// Case 3: Number Cards (2-10)
41+
test(`Should return the numerical value of number cards`, () => {
42+
const cardsNumbers = [2, 3, 4, 5, 6, 7, 8, 9, 10];
43+
for (const rank of cardsNumbers) {
44+
for (const suit of suits) {
45+
const cardFace = `${rank}${suit}`;
46+
expect(getCardValue(cardFace)).toEqual(rank);
47+
}
48+
}
49+
});
50+
51+
// Case 4: Invalid Cards
52+
test(`Should throw an error when given an invalid card`, () => {
53+
const invalidCards = ["invalid", 7, "", "AA♠", "10♠♦"];
54+
for (const invalidCard of invalidCards) {
55+
expect(() => {
56+
getCardValue(invalidCard);
57+
}).toThrow();
58+
}
59+
});
2060

Sprint-3/2-practice-tdd/count.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
let count = 0;
3+
for (let i = 0; i < stringOfCharacters.length; i++) {
4+
if (stringOfCharacters[i] === findCharacter) {
5+
}
6+
}
7+
return count;
38
}
4-
5-
module.exports = countChar;
9+
module.exports = countChar;

0 commit comments

Comments
 (0)