Skip to content

Commit 478b72b

Browse files
committed
coursework compleat
1 parent 990e0cb commit 478b72b

File tree

13 files changed

+176
-50
lines changed

13 files changed

+176
-50
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
// execute the code to ensure all tests pass.
1212

1313
function isProperFraction(numerator, denominator) {
14-
// TODO: Implement this function
14+
if (numerator >= denominator) {
15+
return false;
16+
} else {
17+
return true;
18+
}
1519
}
1620

1721
// The line below allows us to load the isProperFraction function into tests in other files.
@@ -31,3 +35,8 @@ function assertEquals(actualOutput, targetOutput) {
3135

3236
// Example: 1/2 is a proper fraction
3337
assertEquals(isProperFraction(1, 2), true);
38+
assertEquals(isProperFraction(5, 6), true);
39+
assertEquals(isProperFraction(1, 100), true);
40+
assertEquals(isProperFraction(3, 2), false);
41+
assertEquals(isProperFraction(9, 9), false);
42+
assertEquals(isProperFraction(100, 2), false);

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

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,44 @@
2222
// execute the code to ensure all tests pass.
2323

2424
function getCardValue(card) {
25-
// TODO: Implement this function
25+
const cardSuit = card.slice(card.length - 1);
26+
const cardRank = card.slice(0, card.length - 1);
27+
let cardValue;
28+
if (
29+
["♠", "♥", "♦", "♣"].includes(cardSuit) &&
30+
["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"].includes(
31+
cardRank
32+
)
33+
) {
34+
if (cardRank == "1") {
35+
throw "invalid";
36+
}
37+
38+
switch (cardRank) {
39+
case "A":
40+
return 11;
41+
case "K":
42+
case "Q":
43+
case "J":
44+
return 10;
45+
//case "1": throw RangeError("invalid")
46+
default:
47+
return parseInt(cardRank);
48+
}
49+
} else {
50+
throw "invalid";
51+
}
2652
}
2753

2854
// The line below allows us to load the getCardValue function into tests in other files.
2955
// This will be useful in the "rewrite tests with jest" step.
3056
module.exports = getCardValue;
3157

3258
// Helper functions to make our assertions easier to read.
33-
function assertEquals(actualOutput, targetOutput) {
34-
console.assert(
35-
actualOutput === targetOutput,
36-
`Expected ${actualOutput} to equal ${targetOutput}`
37-
);
38-
}
59+
// function assertEquals(actualOutput, targetOutput) {
60+
// console.assert(
61+
// actualOutput === targetOutput,
62+
// `Expected ${actualOutput} to equal ${targetOutput}`
3963

4064
// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
41-
// Examples:
42-
assertEquals(getCardValue("9♠"), 9);
43-
44-
// Handling invalid cards
45-
try {
46-
getCardValue("invalid");
47-
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) {}
51-
52-
// What other invalid card cases can you think of?
65+
//invalid card cases can you think of?

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,31 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
1313
expect(getAngleType(89)).toEqual("Acute angle");
1414
});
1515

16-
// Case 2: Right angle
16+
//Case 2: Right angle
17+
test(`should return "Right angle" when (angle === 90)`, () => {
18+
expect(getAngleType(90)).toEqual("Right angle");
19+
});
20+
1721
// Case 3: Obtuse angles
22+
test(`should return "Obtuse angle" when (90> angle < 180)`,() =>{
23+
expect(getAngleType(91)).toEqual("Obtuse angle")
24+
expect(getAngleType(127)).toEqual("Obtuse angle")
25+
expect(getAngleType(179)).toEqual("Obtuse angle")
26+
});
27+
1828
// Case 4: Straight angle
29+
test(`Should return "Straight angle" when ( angle === 180)`,()=>{
30+
expect(getAngleType(180)).toEqual("Straight angle")
31+
});
32+
1933
// Case 5: Reflex angles
34+
test(`should return "Reflex angle" when (180> angle <=360)`,()=>{
35+
expect(getAngleType(181)).toEqual("Reflex angle")
36+
expect(getAngleType(270)).toEqual("Reflex angle")
37+
expect(getAngleType(359)).toEqual("Reflex angle")
38+
39+
});
2040
// Case 6: Invalid angles
41+
test(`should return "Invalid angle" when(360>)`,()=>{
42+
expect(getAngleType(361)).toEqual("Invalid")
43+
});

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,19 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
77
// Special case: numerator is zero
88
test(`should return false when denominator is zero`, () => {
99
expect(isProperFraction(1, 0)).toEqual(false);
10+
expect(isProperFraction(0,0)).toEqual(false)
11+
});
12+
13+
// Where the numerator is > than the denominator
14+
test(`should return false when the (numerator > denominator)`,()=>{
15+
expect(isProperFraction(4,2)).toEqual(false)
16+
expect(isProperFraction(100,6)).toEqual(false)
17+
expect(isProperFraction(5,-10)).toEqual(false)
18+
});
19+
20+
// Where the fraction is correct
21+
test(`Should return a true when the (numerator < denominator)`,()=>{
22+
expect(isProperFraction(1,2)).toEqual(true)
23+
expect(isProperFraction(6,8)).toEqual(true)
24+
expect(isProperFraction(-1,2)).toEqual(true)
1025
});

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,36 @@ const getCardValue = require("../implement/3-get-card-value");
77
// Case 1: Ace (A)
88
test(`Should return 11 when given an ace card`, () => {
99
expect(getCardValue("A♠")).toEqual(11);
10+
expect(getCardValue("A♣")).toEqual(11);
11+
expect(getCardValue("A♥")).toEqual(11);
12+
expect(getCardValue("A♦")).toEqual(11);
1013
});
1114

1215
// Suggestion: Group the remaining test data into these categories:
1316
// Number Cards (2-10)
17+
test(`Should return 11 when given an ace card`, () => {
18+
expect(getCardValue("7♠")).toEqual(7);
19+
expect(getCardValue("9♣")).toEqual(9);
20+
expect(getCardValue("3♥")).toEqual(3);
21+
expect(getCardValue("5♦")).toEqual(5);
22+
});
1423
// Face Cards (J, Q, K)
15-
// Invalid Cards
24+
test(`Should return 11 when given an ace card`, () => {
25+
expect(getCardValue("J♠")).toEqual(10);
26+
expect(getCardValue("K♣")).toEqual(10);
27+
expect(getCardValue("Q♥")).toEqual(10);
28+
expect(getCardValue("K♦")).toEqual(10);
29+
});
30+
//Invalid Cards
31+
test("throws on invalid card", () => {
32+
expect(() => {
33+
getCardValue("JJ"); }).toThrow("invalid");
34+
expect(() => {
35+
getCardValue("1♠"); }).toThrow("invalid");
36+
expect(() => {
37+
getCardValue("A1"); }).toThrow("invalid");
1638

39+
});
1740
// To learn how to test whether a function throws an error as expected in Jest,
1841
// please refer to the Jest documentation:
1942
// https://jestjs.io/docs/expect#tothrowerror
20-

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
totalCount=stringOfCharacters.split("");
3+
Count=0
4+
for(i=0; i<totalCount.length; i++){
5+
if(totalCount[i]===findCharacter){
6+
Count++
7+
}
38
}
4-
9+
return Count
10+
}
11+
console.log(countChar("AAA","A"))
512
module.exports = countChar;
13+
14+

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,19 @@ const countChar = require("./count");
1111
// Then it should correctly count occurrences of `char`.
1212

1313
test("should count multiple occurrences of a character", () => {
14-
const str = "aaaaa";
15-
const char = "a";
16-
const count = countChar(str, char);
17-
expect(count).toEqual(5);
14+
expect(countChar("aaaaa","a")).toEqual(5);
1815
});
1916

2017
// Scenario: No Occurrences
18+
test(`should return 0 in the instance of no char counted`,() =>{
19+
expect(countChar("","a")).toEqual(0);
20+
});
21+
22+
//});
2123
// Given the input string `str`,
22-
// And a character `char` that does not exist within `str`.
24+
test(` character char that does not exist within str`,()=>{
25+
expect(countChar("aaaaa","b")).toEqual(0);
26+
});
27+
2328
// When the function is called with these inputs,
2429
// Then it should return 0, indicating that no occurrences of `char` were found.
Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
function getOrdinalNumber(num) {
2-
return "1st";
3-
}
2+
if (num >=11 && num <= 19){
3+
return `${num}th`
4+
};
45

6+
const lastDigit = num % 10
7+
switch (lastDigit) {
8+
case 1:
9+
return `${num}st`;
10+
case 2:
11+
return `${num}nd`;
12+
case 3:
13+
return `${num}rd`;
14+
default:
15+
return `${num}th`;
16+
}
17+
}
518
module.exports = getOrdinalNumber;

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,23 @@ test("should append 'st' for numbers ending with 1, except those ending with 11"
1818
expect(getOrdinalNumber(21)).toEqual("21st");
1919
expect(getOrdinalNumber(131)).toEqual("131st");
2020
});
21+
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
22+
expect(getOrdinalNumber(2)).toEqual("2nd");
23+
expect(getOrdinalNumber(22)).toEqual("22nd");
24+
expect(getOrdinalNumber(132)).toEqual("132nd");
25+
});
26+
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
27+
expect(getOrdinalNumber(3)).toEqual("3rd");
28+
expect(getOrdinalNumber(23)).toEqual("23rd");
29+
expect(getOrdinalNumber(133)).toEqual("133rd");
30+
});
31+
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
32+
expect(getOrdinalNumber(4)).toEqual("4th");
33+
expect(getOrdinalNumber(25)).toEqual("25th");
34+
expect(getOrdinalNumber(134)).toEqual("134th");
35+
});
36+
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
37+
expect(getOrdinalNumber(11)).toEqual("11th");
38+
expect(getOrdinalNumber(14)).toEqual("14th");
39+
expect(getOrdinalNumber(18)).toEqual("18th");
40+
});
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
function repeatStr() {
2-
return "hellohellohello";
3-
}
1+
function repeatStr(times, str) {
2+
if (times < 0){ throw ("error")}
3+
return str.repeat(times);
4+
5+
}
6+
47

58
module.exports = repeatStr;

0 commit comments

Comments
 (0)