Skip to content

Commit 7712f22

Browse files
author
Enice-Codes
committed
implemented and rewrote code
1 parent b31a586 commit 7712f22

15 files changed

Lines changed: 267 additions & 16 deletions

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
// Implement a function getAngleType
23
//
34
// When given an angle in degrees, it should return a string indicating the type of angle:
@@ -16,6 +17,19 @@
1617

1718
function getAngleType(angle) {
1819
// TODO: Implement this function
20+
if (angle > 0 && angle < 90)
21+
return "Acute angle";
22+
else if (angle === 90)
23+
return "Right angle";
24+
else if (angle > 90 && angle < 180)
25+
return "obtuse angle";
26+
else if (angle === 180)
27+
return "Straight angle";
28+
else if (angle > 180 && angle < 360)
29+
return "Reflex angle";
30+
else
31+
return "Invalid angle";
32+
1933
}
2034

2135
// The line below allows us to load the getAngleType function into tests in other files.
@@ -35,3 +49,21 @@ function assertEquals(actualOutput, targetOutput) {
3549
// Example: Identify Right Angles
3650
const right = getAngleType(90);
3751
assertEquals(right, "Right angle");
52+
53+
const acute = getAngleType(45);
54+
assertEquals(acute, 'Acute angle');
55+
56+
const obtuse = getAngleType(120);
57+
assertEquals(obtuse,'obtuse angle')
58+
59+
const straight = getAngleType(180);
60+
assertEquals(straight, 'Straight angle');
61+
62+
const reflex = getAngleType(270);
63+
assertEquals(reflex, 'Reflex angle');
64+
65+
const invalid1 = getAngleType(-10);
66+
assertEquals(invalid1, 'Invalid angle');
67+
68+
const invalid2 = getAngleType(400);
69+
assertEquals(invalid2, 'Invalid angle');

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212

1313
function isProperFraction(numerator, denominator) {
1414
// TODO: Implement this function
15+
if (denominator === 0){
16+
return fasle;
17+
18+
}
19+
if (numerator < denominator && numerator > 0){
20+
return true;
21+
}
22+
return false;
1523
}
1624

1725
// The line below allows us to load the isProperFraction function into tests in other files.
@@ -31,3 +39,15 @@ function assertEquals(actualOutput, targetOutput) {
3139

3240
// Example: 1/2 is a proper fraction
3341
assertEquals(isProperFraction(1, 2), true);
42+
43+
// exapmle : 4/3 is not a proper fraction
44+
assertEquals(isProperFraction(4, 3), false);
45+
46+
// example: 6/5 is not a proper fraction
47+
assertEquals(isProperFraction(6, 5), false);
48+
49+
// example: 0/5 is a proper fraction
50+
assertEquals(isProperFraction(0, 5), true);
51+
52+
// example: 5/0 is not a proper fraction
53+
assertEquals(isProperFraction(5, 0), false);

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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@
2323

2424
function getCardValue(card) {
2525
// TODO: Implement this function
26+
if(card === "A♠" || card === "A♥" || card === "A♦" || card === "A♣"){
27+
return 11;
28+
}
29+
else if(card === "J♠" || card === "J♥" || card === "J♦" || card === "J♣" || card === "Q♠" || card === "Q♥" || card === "Q♦" || card === "Q♣" || card === "K♠" || card === "K♥" || card === "K♦" || card === "K♣"){
30+
return 10;
31+
}
32+
else if(card === "2♠" || card === "2♥" || card === "2♦" || card === "2♣"){
33+
return 2;
34+
}
35+
else if(card === "3♠" || card === "3♥" || card === "3♦" || card === "3♣"){
36+
return 3;
37+
}
38+
else {
39+
throw new Error("Invalid card");
40+
}
2641
}
2742

2843
// The line below allows us to load the getCardValue function into tests in other files.
@@ -46,9 +61,34 @@ try {
4661
getCardValue("invalid");
4762

4863
// This line will not be reached if an error is thrown as expected
49-
console.error("Error was not thrown for invalid card 😢");
64+
console.error("Error was not thrown for invalid card ");
5065
} catch (e) {
51-
console.log("Error thrown for invalid card 🎉");
66+
console.log("Error thrown for invalid card ");
5267
}
5368

69+
assertEquals(getCardValue("A♠"), 11);
70+
assertEquals(getCardValue("J♣"), 10);
71+
assertEquals(getCardValue("Q♦"), 10);
72+
assertEquals(getCardValue("K♥"), 10);
73+
assertEquals(getCardValue("2♠"), 2);
74+
assertEquals(getCardValue("3♥"), 3);
75+
76+
// What other valid card cases can you think of?
77+
function assertEqual(func, valid message){
78+
try{
79+
func();
80+
console.log("Valid card test passed ");
81+
}
5482
// What other invalid card cases can you think of?
83+
function assertThrows(func, errorMessage) {
84+
try {
85+
func();
86+
console.error("Error was not thrown ");
87+
} catch (e) {
88+
if (e.message === errorMessage) {
89+
console.log("Error thrown as expected ");
90+
} else {
91+
console.error(`Unexpected error message: ${e.message}`);
92+
}
93+
}
94+
}

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,36 @@ const getAngleType = require("../implement/1-get-angle-type");
66
// including boundary and invalid cases.
77

88
// Case 1: Acute angles
9-
test(`should return "Acute angle" when (0 < angle < 90)`, () => {
9+
test(`should return "Acute angle" when (0 < angle && angle < 90)`, () => {
1010
// Test various acute angles, including boundary cases
1111
expect(getAngleType(1)).toEqual("Acute angle");
1212
expect(getAngleType(45)).toEqual("Acute angle");
1313
expect(getAngleType(89)).toEqual("Acute angle");
1414
});
1515

1616
// Case 2: Right angle
17+
test (`should return "right angle" when (angle === 90)`,() => {
18+
// testing various angles ,including boundary cases
19+
expect( getAngleType(90)).toEqual("right angle");
20+
});
1721
// Case 3: Obtuse angles
22+
test (`should return "obtuse angle" when (90 < angle && angle < 180)`, () => {
23+
expect(getAngleType(91)).toEqual("obtuse angle");
24+
expect(getAngleType(125)).toEqual("obtuse angle");
25+
expect(getAngleType(172)).toEqual("obtuse angle");
26+
});
1827
// Case 4: Straight angle
19-
// Case 5: Reflex angles
28+
test(`should return "straight angle" when (angle === 180)`, () => {
29+
expect(getAngleType(180)).toEqual("straight angle");
30+
});
31+
// Case 5: Reflex angles\
32+
test(`should return "reflex angle" when (180 < angle && angle < 360)`, () => {
33+
expect(getAngleType(181)).toEqual("reflex angle");
34+
expect(getAngleType(270)).toEqual("reflex angle");
35+
expect(getAngleType(359)).toEqual("reflex angle");
36+
});
2037
// Case 6: Invalid angles
38+
test(`should return "Invalid angle" when (angle < 0 || angle > 360)`, () => {
39+
expect(getAngleType(-1)).toEqual("Invalid angle");
40+
expect(getAngleType(361)).toEqual("Invalid angle");
41+
});

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,17 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
88
test(`should return false when denominator is zero`, () => {
99
expect(isProperFraction(1, 0)).toEqual(false);
1010
});
11+
12+
// special case : numerator is one
13+
test(` should return true when the numerator is one and denominator is greater than one`, () => {
14+
expect(isProperFraction(1, 2)).toEqual(true);
15+
});
16+
// special case : denominator is negative to the numerator
17+
test(`should return false when the denominator is negative and the numerator is positive`, () => {
18+
expect(isProperFraction(1, -2)).toEqual(false);
19+
});
20+
21+
// special case : denominator is negative to the numerator
22+
test(`should return false when the numerator is negative and the denominator is positive`, () => {
23+
expect(isProperFraction(-1, 2)).toEqual(false);
24+
});

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,24 @@ test(`Should return 11 when given an ace card`, () => {
99
expect(getCardValue("A♠")).toEqual(11);
1010
});
1111

12-
// Suggestion: Group the remaining test data into these categories:
13-
// Number Cards (2-10)
14-
// Face Cards (J, Q, K)
15-
// Invalid Cards
12+
// case 2 : face cards (J, Q, K)
13+
test(`Should return 10 when given a face card`, () => {
14+
expect(getCardValue("J♠")).toEqual(10);
15+
expect(getCardValue("Q♥")).toEqual(10);
16+
expect(getCardValue("K♦")).toEqual(10);
17+
});
18+
19+
// Case 3: Number Cards (2-10)
20+
test(`Should return the correct value when given a number card`, () => {
21+
expect(getCardValue("2♠")).toEqual(2);
22+
expect(getCardValue("5♥")).toEqual(5);
23+
expect(getCardValue("10♦")).toEqual(10);
24+
});
25+
26+
// Case 4: Invalid Cards
27+
test(`Should throw an error when given an invalid card`, () => {
28+
expect(() => getCardValue("invalid")).toThrow("Invalid card");
29+
});
1630

1731
// To learn how to test whether a function throws an error as expected in Jest,
1832
// please refer to the Jest documentation:

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@ function countChar(stringOfCharacters, findCharacter) {
33
}
44

55
module.exports = countChar;
6+
test( "should count multiple occurrences of a character ", function(){
7+
const stringofcharacters = "aaaaa";
8+
const findcharcaters = "a";
9+
const count = countChar(stringofcharacters, findcharcaters);
10+
expect(count).toEqual(5);
11+
});

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ const countChar = require("./count");
33
// Given a string `str` and a single character `char` to search for,
44
// When the countChar function is called with these inputs,
55
// Then it should:
6+
function CountChar(str, char) {
7+
let count = 0;
8+
for (let i = 0; i < str.length; i++) {
9+
if (str[i] === char) {
10+
count++;
11+
}
12+
}
13+
return count;
14+
}
615

716
// Scenario: Multiple Occurrences
817
// Given the input string `str`,
@@ -22,3 +31,9 @@ test("should count multiple occurrences of a character", () => {
2231
// And a character `char` that does not exist within `str`.
2332
// When the function is called with these inputs,
2433
// Then it should return 0, indicating that no occurrences of `char` were found.
34+
test('should return 0 when the character does not occur in the string', () => {
35+
const str = "hello";
36+
const char = "x";
37+
const count = countChar(str, char);
38+
expect(count).toEqual(5);
39+
});

Sprint-3/2-practice-tdd/get-ordinal-number.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,13 @@ function getOrdinalNumber(num) {
33
}
44

55
module.exports = getOrdinalNumber;
6+
test(`works with any number ending with 1, except for 11. For all other numbers, it should return the number followed by "th" with exceptions to 2 and 3 `, () => {
7+
expect(getOrdinalNumber(1)).toBe("1st");
8+
expect(getOrdinalNumber(2)).toBe("2nd");
9+
expect(getOrdinalNumber(3)).toBe("3rd");
10+
expect(getOrdinalNumber(4)).toBe("4th");
11+
expect(getOrdinalNumber(11)).toBe("11th");
12+
expect(getOrdinalNumber(12)).toBe("12th");
13+
});
14+
15+

Sprint-3/2-practice-tdd/get-ordinal-number.test.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ const getOrdinalNumber = require("./get-ordinal-number");
1515
// Then the function should return a string by appending "st" to the number.
1616
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
1717
expect(getOrdinalNumber(1)).toEqual("1st");
18-
expect(getOrdinalNumber(21)).toEqual("21st");
18+
expect(getOrdinalNumber(31)).toEqual("31st");
1919
expect(getOrdinalNumber(131)).toEqual("131st");
2020
});
21+
test(`should append 'nd' for numbers ending with 2 except those ending with 12 `, () => {
22+
expect(getOrdinalNumber(2)).toEqual("2nd");
23+
expect(getOrdinalNumber(22)).toEqual("22nd");
24+
expect(getOrdinalNumber(132)).toEqual("132nd");
25+
});
26+
27+
test(`should append 'rd' for numbers ending with 3 except those ending with 13 `, () => {
28+
expect(getOrdinalNumber(3)).toEqual("3rd");
29+
expect(getOrdinalNumber(23)).toEqual("23rd");
30+
expect(getOrdinalNumber(143)).toEqual("143rd");
31+
});
32+

0 commit comments

Comments
 (0)