Skip to content

Commit 0048729

Browse files
author
Enice-Codes
committed
modified excercise
1 parent 7712f22 commit 0048729

12 files changed

Lines changed: 182 additions & 121 deletions

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,5 @@ const invalid1 = getAngleType(-10);
6666
assertEquals(invalid1, 'Invalid angle');
6767

6868
const invalid2 = getAngleType(400);
69-
assertEquals(invalid2, 'Invalid angle');
69+
assertEquals(invalid2, 'Invalid angle');
70+

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,21 @@
99
// Acceptance criteria:
1010
// After you have implemented the function, write tests to cover all the cases, and
1111
// execute the code to ensure all tests pass.
12-
1312
function isProperFraction(numerator, denominator) {
14-
// TODO: Implement this function
15-
if (denominator === 0){
16-
return fasle;
17-
13+
if (denominator === 0) {
14+
return false;
1815
}
19-
if (numerator < denominator && numerator > 0){
16+
if (numerator < denominator && numerator >= 0) {
2017
return true;
2118
}
2219
return false;
2320
}
2421

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

24+
// The line below allows us to load the isProperFraction function into tests in other files.
25+
// This will be useful in the "rewrite tests with jest" step
26+
2927
// Here's our helper again
3028
function assertEquals(actualOutput, targetOutput) {
3129
console.assert(
@@ -49,5 +47,3 @@ assertEquals(isProperFraction(6, 5), false);
4947
// example: 0/5 is a proper fraction
5048
assertEquals(isProperFraction(0, 5), true);
5149

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: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,30 @@
2020
// Acceptance criteria:
2121
// After you have implemented the function, write tests to cover all the cases, and
2222
// execute the code to ensure all tests pass.
23-
2423
function getCardValue(card) {
25-
// 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;
24+
if (typeof card !== "string") {
25+
throw new Error("Invalid card");
3726
}
38-
else {
27+
28+
const suits = ["♠", "♥", "♦", "♣"];
29+
const suit = card.slice(-1);
30+
const rank = card.slice(0, -1);
31+
32+
if (!suits.includes(suit)) {
3933
throw new Error("Invalid card");
4034
}
35+
36+
if (rank === "A") return 11;
37+
if (rank === "J" || rank === "Q" || rank === "K") return 10;
38+
39+
const num = Number(rank);
40+
if (Number.isInteger(num) && num >= 2 && num <= 10 && String(num) === rank) {
41+
return num;
42+
}
43+
44+
throw new Error("Invalid card");
4145
}
4246

43-
// The line below allows us to load the getCardValue function into tests in other files.
44-
// This will be useful in the "rewrite tests with jest" step.
4547
module.exports = getCardValue;
4648

4749
// Helper functions to make our assertions easier to read.
@@ -72,23 +74,45 @@ assertEquals(getCardValue("Q♦"), 10);
7274
assertEquals(getCardValue("K♥"), 10);
7375
assertEquals(getCardValue("2♠"), 2);
7476
assertEquals(getCardValue("3♥"), 3);
75-
77+
7678
// What other valid card cases can you think of?
77-
function assertEqual(func, valid message){
78-
try{
79+
function assertValid(func, description) {
80+
try {
7981
func();
80-
console.log("Valid card test passed ");
81-
}
82+
console.log(`Valid card test passed: ${description}`);
83+
} catch (e) {
84+
console.error(`Valid card test FAILED (threw unexpectedly): ${description} - ${e.message}`);
85+
}
86+
}
87+
8288
// What other invalid card cases can you think of?
83-
function assertThrows(func, errorMessage) {
89+
function assertThrows(func, description) {
8490
try {
8591
func();
86-
console.error("Error was not thrown ");
92+
console.error(`Error was not thrown for: ${description}`);
8793
} 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-
}
94+
console.log(`Error thrown as expected for: ${description}`);
9395
}
94-
}
96+
}
97+
98+
// What other valid card cases can you think of?
99+
assertValid(() => assertEquals(getCardValue("10♦"), 10), "10♦ (double-digit rank)");
100+
assertValid(() => assertEquals(getCardValue("A♣"), 11), "A♣");
101+
assertValid(() => assertEquals(getCardValue("K♠"), 10), "K♠");
102+
assertValid(() => assertEquals(getCardValue("7♥"), 7), "7♥");
103+
assertValid(() => assertEquals(getCardValue("2♦"), 2), "2♦");
104+
105+
// What other invalid card cases can you think of?
106+
assertThrows(() => getCardValue(""), "empty string");
107+
assertThrows(() => getCardValue("1♠"), "rank 1 doesn't exist");
108+
assertThrows(() => getCardValue("11♠"), "rank 11 doesn't exist");
109+
assertThrows(() => getCardValue("A"), "missing suit");
110+
assertThrows(() => getCardValue("♠"), "missing rank");
111+
assertThrows(() => getCardValue("AS"), "letter suit instead of emoji");
112+
assertThrows(() => getCardValue("a♠"), "lowercase rank");
113+
assertThrows(() => getCardValue("K♠ "), "trailing whitespace");
114+
assertThrows(() => getCardValue(null), "null input");
115+
assertThrows(() => getCardValue(10), "number input");
116+
117+
console.log("\nAll tests completed.");
118+
Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
11
// This statement loads the getAngleType function you wrote in the implement directory.
22
// We will use the same function, but write tests for it using Jest in this file.
33
const getAngleType = require("../implement/1-get-angle-type");
4-
54
// TODO: Write tests in Jest syntax to cover all cases/outcomes,
65
// including boundary and invalid cases.
76

87
// Case 1: Acute angles
9-
test(`should return "Acute angle" when (0 < angle && angle < 90)`, () => {
10-
// Test various acute angles, including boundary cases
8+
test(`should return "Acute angle" when (0 < angle && angle < 90)`, () => {
119
expect(getAngleType(1)).toEqual("Acute angle");
1210
expect(getAngleType(45)).toEqual("Acute angle");
1311
expect(getAngleType(89)).toEqual("Acute angle");
1412
});
1513

1614
// 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-
});
15+
test(`should return "Right angle" when (angle === 90)`, () => {
16+
expect(getAngleType(90)).toEqual("Right angle");
17+
});
18+
2119
// Case 3: Obtuse angles
2220
test (`should return "obtuse angle" when (90 < angle && angle < 180)`, () => {
2321
expect(getAngleType(91)).toEqual("obtuse angle");
2422
expect(getAngleType(125)).toEqual("obtuse angle");
2523
expect(getAngleType(172)).toEqual("obtuse angle");
2624
});
25+
2726
// Case 4: Straight angle
28-
test(`should return "straight angle" when (angle === 180)`, () => {
29-
expect(getAngleType(180)).toEqual("straight angle");
27+
test(`should return "Straight angle" when (angle === 180)`, () => {
28+
expect(getAngleType(180)).toEqual("Straight angle");
3029
});
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");
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");
3636
});
37+
3738
// Case 6: Invalid angles
3839
test(`should return "Invalid angle" when (angle < 0 || angle > 360)`, () => {
3940
expect(getAngleType(-1)).toEqual("Invalid angle");
4041
expect(getAngleType(361)).toEqual("Invalid angle");
41-
});
42+
});

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ test(` should return true when the numerator is one and denominator is greater t
1414
expect(isProperFraction(1, 2)).toEqual(true);
1515
});
1616
// 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);
17+
test(`should return false when denominator is zero`, () => {
18+
expect(isProperFraction(1, 0)).toEqual(false);
1919
});
2020

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-
});
21+
test(`should return true when the numerator is one and denominator is greater than one`, () => {
22+
expect(isProperFraction(1, 2)).toEqual(true);
23+
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ test(`Should return the correct value when given a number card`, () => {
2525

2626
// Case 4: Invalid Cards
2727
test(`Should throw an error when given an invalid card`, () => {
28-
expect(() => getCardValue("invalid")).toThrow("Invalid card");
28+
expect(() => getCardValue("invalid")).toThrow(new Error("Invalid card"));
2929
});
3030

3131
// To learn how to test whether a function throws an error as expected in Jest,
Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
2+
const lastdigit= num % 10
3+
switch (lastdigit){
4+
case 1:
5+
console.log("st");
6+
return `${num}st`;
7+
8+
case 2 :
9+
console.log("nd");
10+
return `${num}nd`;
11+
12+
case 3:
13+
console.log("rd");
14+
return `${num}rd`;
15+
16+
17+
}
318
}
419

5-
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-
});
1420

21+
module.exports = getOrdinalNumber;
22+
1523

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const getOrdinalNumber = require("./get-ordinal-number");
2+
3+
24
// In this week's prep, we started implementing getOrdinalNumber.
35

46
// Continue testing and implementing getOrdinalNumber for additional cases.
@@ -14,19 +16,24 @@ const getOrdinalNumber = require("./get-ordinal-number");
1416
// When the number ends with 1, except those ending with 11,
1517
// Then the function should return a string by appending "st" to the number.
1618
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
17-
expect(getOrdinalNumber(1)).toEqual("1st");
18-
expect(getOrdinalNumber(31)).toEqual("31st");
19-
expect(getOrdinalNumber(131)).toEqual("131st");
19+
expect(getOrdinalNumber(1)).toBe("1st");
20+
expect(getOrdinalNumber(21)).toBe("21st");
21+
expect(getOrdinalNumber(131)).toBe("131st");
22+
2023
});
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");
24+
25+
// case 2: Numbers ending with 2 ,but not 12
26+
test("should append 'nd' for numbers ending with 2 ,except those ending with 12 ", () => {
27+
expect(getOrdinalNumber(2)).toBe("2nd");
28+
expect(getOrdinalNumber(22)).toBe("22nd");
29+
expect(getOrdinalNumber(132)).toBe("132nd");
2530
});
2631

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");
32+
// case 3 : Numbers ending with 3 ,but not 13
33+
test("should append 'rd'for numbers ending with 3 ,except those ending with 13 ", () => {
34+
expect(getOrdinalNumber(3)).toBe("3rd");
35+
expect(getOrdinalNumber(23)).toBe("23rd");
36+
expect(getOrdinalNumber(133)).toBe("133rd");
37+
3138
});
32-
39+
Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
1-
function repeatStr() {
1+
function repeatStr(str, num) {
2+
if (num < 0) {
3+
throw new Error("Count cannot be negative");
4+
}
5+
6+
7+
let result = "";
8+
for (let i=0; i < num; i ++){
9+
result += str;
10+
}
11+
12+
return result;
13+
14+
}
15+
16+
console.log(repeatStr("hello",3));
217
// Your implementation of this function must *not* call String.prototype.repeat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat).
318
// The goal is to re-implement that function, not to use it.
4-
return "hellohellohello";
5-
}
19+
20+
621

722
module.exports = repeatStr;
23+
24+

Sprint-3/2-practice-tdd/repeat-str.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,30 @@ test("should repeat the string count times", () => {
2121
// When the repeatStr function is called with these inputs,
2222
// Then it should return the original `str` without repetition.
2323

24+
test("should return the original str without repetition", () => {
25+
const str = "hello";
26+
const count = 1;
27+
const repeatedStr = repeatStr(str, count);
28+
expect(repeatedStr).toEqual("hello");
29+
});
30+
2431
// Case: Handle count of 0:
2532
// Given a target string `str` and a `count` equal to 0,
2633
// When the repeatStr function is called with these inputs,
2734
// Then it should return an empty string.
2835

36+
test("should return an empty string", () => {
37+
const str = "hello";
38+
const count = 0;
39+
const repeatedStr = repeatStr(str, count);
40+
expect(repeatedStr).toEqual("");
41+
});
42+
2943
// Case: Handle negative count:
3044
// Given a target string `str` and a negative integer `count`,
3145
// When the repeatStr function is called with these inputs,
3246
// Then it should throw an error, as negative counts are not valid.
47+
48+
test("should throw an error because negative counts are not valid", () => {
49+
expect(() => repeatStr("hello", -3 )).toThrow();
50+
});

0 commit comments

Comments
 (0)