Skip to content

Commit df175c6

Browse files
committed
Completed all my JS exercises with implementations and Jest tests
1 parent 237a6b1 commit df175c6

File tree

15 files changed

+325
-43
lines changed

15 files changed

+325
-43
lines changed

Sprint-3/1-implement-and-rewrite-tests/implement/1-get-angle-type.js

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,29 @@
88
// Then, write the next test! :) Go through this process until all the cases are implemented
99

1010
function getAngleType(angle) {
11-
if (angle === 90) {
11+
12+
if (angle ===90){
13+
1214
return "Right angle";
15+
16+
} else if (angle <90){
17+
18+
return "Acute angle";
19+
20+
} else if (angle >90 && angle <180){
21+
22+
return "Obtuse angle";
23+
24+
} else if (angle ===180){
25+
26+
return "Straight angle";
27+
28+
} else if (angle >180 && angle <360){
29+
30+
return "Reflex angle";
1331
}
14-
// Run the tests, work out what Case 2 is testing, and implement the required code here.
15-
// Then keep going for the other cases, one at a time.
32+
// Run the tests, work out what Case 2 is testing, and implement the required code here.
33+
// Then keep going for the other cases, one at a time.
1634
}
1735

1836
// The line below allows us to load the getAngleType function into tests in other files.
@@ -21,9 +39,11 @@ module.exports = getAngleType;
2139

2240
// we're going to use this helper function to make our assertions easier to read
2341
// if the actual output matches the target output, the test will pass
42+
2443
function assertEquals(actualOutput, targetOutput) {
2544
console.assert(
2645
actualOutput === targetOutput,
46+
2747
`Expected ${actualOutput} to equal ${targetOutput}`
2848
);
2949
}
@@ -37,27 +57,42 @@ function assertEquals(actualOutput, targetOutput) {
3757
// Case 1: Identify Right Angles:
3858
// When the angle is exactly 90 degrees,
3959
// Then the function should return "Right angle"
60+
4061
const right = getAngleType(90);
62+
4163
assertEquals(right, "Right angle");
4264

4365
// Case 2: Identify Acute Angles:
4466
// When the angle is less than 90 degrees,
4567
// Then the function should return "Acute angle"
68+
4669
const acute = getAngleType(45);
70+
4771
assertEquals(acute, "Acute angle");
4872

4973
// Case 3: Identify Obtuse Angles:
5074
// When the angle is greater than 90 degrees and less than 180 degrees,
5175
// Then the function should return "Obtuse angle"
76+
5277
const obtuse = getAngleType(120);
78+
79+
assertEquals(obtuse, "Obtuse angle");
5380
// ====> write your test here, and then add a line to pass the test in the function above
5481

5582
// Case 4: Identify Straight Angles:
5683
// When the angle is exactly 180 degrees,
5784
// Then the function should return "Straight angle"
5885
// ====> write your test here, and then add a line to pass the test in the function above
5986

87+
const straight = getAngleType(180);
88+
89+
assertEquals(straight, "Straight angle");
90+
6091
// Case 5: Identify Reflex Angles:
6192
// When the angle is greater than 180 degrees and less than 360 degrees,
6293
// Then the function should return "Reflex angle"
63-
// ====> write your test here, and then add a line to pass the test in the function above
94+
// ====> write your test here, and then add a line to pass the test in the function above
95+
96+
const reflex = getAngleType(270);
97+
98+
assertEquals(reflex, "Reflex angle");

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

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,32 @@
77
// complete the rest of the tests and cases
88
// write one test at a time, and make it pass, build your solution up methodically
99

10-
function isProperFraction(numerator, denominator) {
11-
if (numerator < denominator) {
10+
function isProperFraction(numerator,denominator) {
11+
12+
if (numerator <denominator){
13+
1214
return true;
15+
}
16+
17+
else {
18+
19+
return false;
1320
}
1421
}
1522

1623
// The line below allows us to load the isProperFraction function into tests in other files.
1724
// This will be useful in the "rewrite tests with jest" step.
18-
module.exports = isProperFraction;
25+
module.exports =isProperFraction;
1926

2027
// here's our helper again
21-
function assertEquals(actualOutput, targetOutput) {
28+
29+
function assertEquals(actualOutput,targetOutput){
30+
2231
console.assert(
23-
actualOutput === targetOutput,
24-
`Expected ${actualOutput} to equal ${targetOutput}`
32+
33+
actualOutput ===targetOutput,
34+
35+
`Expected ${actualOutput} to equal${targetOutput}`
2536
);
2637
}
2738

@@ -31,29 +42,43 @@ function assertEquals(actualOutput, targetOutput) {
3142
// Input: numerator = 2, denominator = 3
3243
// target output: true
3344
// Explanation: The fraction 2/3 is a proper fraction, where the numerator is less than the denominator. The function should return true.
34-
const properFraction = isProperFraction(2, 3);
35-
assertEquals(properFraction, true);
45+
46+
const properFraction =isProperFraction(2,3);
47+
48+
assertEquals(properFraction,true);
49+
3650

3751
// Improper Fraction check:
3852
// Input: numerator = 5, denominator = 2
3953
// target output: false
4054
// 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.
41-
const improperFraction = isProperFraction(5, 2);
42-
assertEquals(improperFraction, false);
55+
56+
const improperFraction =isProperFraction(5,2);
57+
58+
assertEquals(improperFraction,false);
59+
4360

4461
// Negative Fraction check:
4562
// Input: numerator = -4, denominator = 7
4663
// target output: true
4764
// 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.
48-
const negativeFraction = isProperFraction(-4, 7);
4965
// ====> complete with your assertion
5066

67+
const negativeFraction =isProperFraction(-4,7);
68+
69+
assertEquals(negativeFraction,true);
70+
71+
5172
// Equal Numerator and Denominator check:
5273
// Input: numerator = 3, denominator = 3
5374
// target output: false
5475
// Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false.
55-
const equalFraction = isProperFraction(3, 3);
5676
// ====> complete with your assertion
5777

78+
const equalFraction =isProperFraction(3,3);
79+
80+
assertEquals(equalFraction,false);
81+
82+
5883
// Stretch:
5984
// What other scenarios could you test for?

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,46 @@
77
// complete the rest of the tests and cases
88
// write one test at a time, and make it pass, build your solution up methodically
99
// just make one change at a time -- don't rush -- programmers are deep and careful thinkers
10-
function getCardValue(card) {
11-
if (rank === "A") {
12-
return 11;
13-
}
10+
11+
function getCardValue(card){
12+
const rank = card.slice(0, -1);
13+
14+
if (rank === "A") return11;
15+
16+
if (["10", "J", "Q", "K"].includes(rank)) return 10;
17+
18+
19+
if (["2","3","4","5","6","7","8","9"].includes(rank)) return Number(rank);
20+
21+
throw new Error("Invalid card rank");
1422
}
1523

24+
module.exports = getCardValue;
25+
1626
// The line below allows us to load the getCardValue function into tests in other files.
1727
// This will be useful in the "rewrite tests with jest" step.
18-
module.exports = getCardValue;
1928

2029
// You need to write assertions for your function to check it works in different cases
2130
// we're going to use this helper function to make our assertions easier to read
2231
// if the actual output matches the target output, the test will pass
23-
function assertEquals(actualOutput, targetOutput) {
24-
console.assert(
25-
actualOutput === targetOutput,
26-
`Expected ${actualOutput} to equal ${targetOutput}`
27-
);
32+
33+
function assertEquals(actual, expected){
34+
35+
console.assert(actual === expected, `Expected ${actual} to equal ${expected}`);
36+
2837
}
38+
2939
// Acceptance criteria:
3040

3141
// 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),
3242
// When the function getCardValue is called with this card string as input,
3343
// Then it should return the numerical card value
34-
const aceofSpades = getCardValue("A♠");
35-
assertEquals(aceofSpades, 11);
3644

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

4452
// Handle Face Cards (J, Q, K):
@@ -55,3 +63,4 @@ const fiveofHearts = getCardValue("5♥");
5563
// Given a card with an invalid rank (neither a number nor a recognized face card),
5664
// When the function is called with such a card,
5765
// Then it should throw an error indicating "Invalid card rank."
66+

Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,31 @@ test("should identify right angle (90°)", () => {
1212
// Case 2: Identify Acute Angles:
1313
// When the angle is less than 90 degrees,
1414
// Then the function should return "Acute angle"
15+
test("should identify acute angles (<90°)",() =>{
1516

17+
expect(getAngleType(45)).toEqual("Acute angle");
18+
});
1619
// Case 3: Identify Obtuse Angles:
1720
// When the angle is greater than 90 degrees and less than 180 degrees,
1821
// Then the function should return "Obtuse angle"
22+
test("should identify obtuse angles (90°<angle<180°)",() =>{
23+
24+
expect(getAngleType(120)).toEqual("Obtuse angle");
25+
});
1926

2027
// Case 4: Identify Straight Angles:
2128
// When the angle is exactly 180 degrees,
2229
// Then the function should return "Straight angle"
30+
test("should identify straight angles (180°)",() =>{
31+
32+
expect(getAngleType(180)).toEqual("Straight angle");
33+
});
2334

2435
// Case 5: Identify Reflex Angles:
2536
// When the angle is greater than 180 degrees and less than 360 degrees,
2637
// Then the function should return "Reflex angle"
38+
39+
test("should identify reflex angles (180°<angle<360°)",() =>{
40+
41+
expect(getAngleType(270)).toEqual("Reflex angle");
42+
});

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@
22
// We will use the same function, but write tests for it using Jest in this file.
33
const isProperFraction = require("../implement/2-is-proper-fraction");
44

5-
test("should return true for a proper fraction", () => {
5+
test("should return true for a proper fraction",() => {
66
expect(isProperFraction(2, 3)).toEqual(true);
77
});
88

99
// Case 2: Identify Improper Fractions:
10+
test("should return false for an improper fraction (numerator > denominator)",()=>{
11+
12+
expect(isProperFraction(5, 2)).toEqual(false);
13+
});
1014

1115
// Case 3: Identify Negative Fractions:
16+
test("should return true for a negative proper fraction (|numerator| < denominator)",()=>{
17+
18+
expect(isProperFraction(-4, 7)).toEqual(true);
19+
});
1220

1321
// Case 4: Identify Equal Numerator and Denominator:
22+
test("should return false when numerator equals denominator",()=>{
23+
24+
expect(isProperFraction(3, 3)).toEqual(false);
25+
});

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,45 @@
22
// We will use the same function, but write tests for it using Jest in this file.
33
const getCardValue = require("../implement/3-get-card-value");
44

5-
test("should return 11 for Ace of Spades", () => {
5+
test("should return 11 for Ace of Spades",() =>{
66
const aceofSpades = getCardValue("A♠");
77
expect(aceofSpades).toEqual(11);
88
});
99

1010
// Case 2: Handle Number Cards (2-10):
11+
test("should return the number value for number cards",() =>{
12+
13+
expect(getCardValue("5♥")).toEqual(5);
14+
15+
expect(getCardValue("10♦")).toEqual(10);
16+
17+
});
18+
1119
// Case 3: Handle Face Cards (J, Q, K):
20+
test("should return 10 for face cards J, Q, K",() =>{
21+
22+
expect(getCardValue("J♣")).toEqual(10);
23+
24+
expect(getCardValue("Q♠")).toEqual(10);
25+
26+
expect(getCardValue("K♦")).toEqual(10);
27+
28+
});
29+
1230
// Case 4: Handle Ace (A):
31+
test("should return 11 for any Ace",() =>{
32+
33+
expect(getCardValue("A♦")).toEqual(11);
34+
35+
expect(getCardValue("A♥")).toEqual(11);
36+
37+
});
38+
1339
// Case 5: Handle Invalid Cards:
40+
test("Invalid cards throw error",() =>{
41+
42+
expect(() => getCardValue("Z♠")).toThrow("Invalid card rank");
43+
44+
expect(() => getCardValue("1♣")).toThrow("Invalid card rank");
45+
46+
});

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

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

516
module.exports = countChar;
17+
18+
19+
20+
21+

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,19 @@ test("should count multiple occurrences of a character", () => {
2222
// And a character char that does not exist within the case-sensitive str,
2323
// When the function is called with these inputs,
2424
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
25+
26+
test("count a's in aaaaa",()=>{
27+
expect(countChar("aaaaa","a")).toEqual(5);
28+
});
29+
30+
test("count z in hello",()=>{
31+
expect(countChar("hello","z")).toEqual(0);
32+
});
33+
34+
test("count b in abc",()=>{
35+
expect(countChar("abc","b")).toEqual(1);
36+
});
37+
38+
test("count a in empty string",()=>{
39+
expect(countChar("","a")).toEqual(0);
40+
});

0 commit comments

Comments
 (0)