Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,20 @@
// execute the code to ensure all tests pass.

function getAngleType(angle) {
// TODO: Implement this function
switch (true) {
case angle == 90:
return "Right angle";
case 0 < angle && angle < 90:
return "Acute angle";
case 90 < angle && angle < 180:
return "Obtuse angle";
case angle == 180:
return "Straight angle";
case 180 < angle && angle < 360:
return "Reflex angle";
case angle > 360 || angle < 0 || angle == 0:
return "Invalid angle";
}
}

// The line below allows us to load the getAngleType function into tests in other files.
Expand All @@ -33,5 +46,45 @@ function assertEquals(actualOutput, targetOutput) {

// TODO: Write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles

const acute1 = getAngleType(1);
assertEquals(acute1, "Acute angle");

const acute45 = getAngleType(45);
assertEquals(acute45, "Acute angle");

const acute89 = getAngleType(89);
assertEquals(acute89, "Acute angle");

const right = getAngleType(90);
assertEquals(right, "Right angle");

const obtuse91 = getAngleType(91);
assertEquals(obtuse91, "Obtuse angle");

const obtuse140 = getAngleType(140);
assertEquals(obtuse140, "Obtuse angle");

const obtuse179 = getAngleType(179);
assertEquals(obtuse179, "Obtuse angle");

const straight = getAngleType(180);
assertEquals(straight, "Straight angle");

const reflex181 = getAngleType(181);
assertEquals(reflex181, "Reflex angle");

const reflex250 = getAngleType(250);
assertEquals(reflex250, "Reflex angle");

const reflex359 = getAngleType(359);
assertEquals(reflex359, "Reflex angle");

const zero = getAngleType(0);
assertEquals(zero, "Invalid angle");

const invalid = getAngleType(400);
assertEquals(invalid, "Invalid angle");

const negative = getAngleType(-4);
assertEquals(zero, "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
switch (true) {
case Math.abs(numerator / denominator) < 1 &&
Math.abs(numerator / denominator) > 0:
return true;
default:
return false;
}
}

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

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(2, 1), false);
assertEquals(isProperFraction(-2, 1), false);
assertEquals(isProperFraction(1, -2), true);
assertEquals(isProperFraction(-2, -4), true);
assertEquals(isProperFraction(-4, -2), false);
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,46 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
const suit = card.replaceAll(/[\w\d\s]/g, "");
const value = card.replaceAll(/[^\w\d\s]/g, "");
const face = ["J", "Q", "K"];
const validSuits = ["♠", "♥", "♦", "♣"];
const validValue = [
"A",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
"J",
"Q",
"K",
];

if (
!validSuits.includes(suit) ||
!validValue.includes(value) ||
card !== value + suit
) {
throw new Error("Invalid card");
}

if (validSuits.includes(suit)) {
switch (true) {
case Number(value) <= 10 && Number(value) >= 2:
return Number(value);
case value === "A":
return 11;
case face.includes(value):
return 10;
}
}

return "Invalid card";
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -39,7 +78,23 @@ function assertEquals(actualOutput, targetOutput) {

// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:
assertEquals(getCardValue("9♠"), 9);

// Test number cards
assertEquals(getCardValue("2♠"), 2);
assertEquals(getCardValue("5♥"), 5);
assertEquals(getCardValue("9♦"), 9);
assertEquals(getCardValue("10♣"), 10);

// Test aces
assertEquals(getCardValue("A♠"), 11);
assertEquals(getCardValue("A♥"), 11);
assertEquals(getCardValue("A♦"), 11);
assertEquals(getCardValue("A♣"), 11);

// Test face cards
assertEquals(getCardValue("J♠"), 10);
assertEquals(getCardValue("Q♥"), 10);
assertEquals(getCardValue("K♦"), 10);

// Handling invalid cards
try {
Expand All @@ -52,3 +107,43 @@ try {
}

// What other invalid card cases can you think of?

function errorThrown(card) {
try {
getCardValue(card);
console.error(`Expected "${card}" to throw an error 😢`);
} catch (error) {
console.log(`"${card}" Error thrown for invalid card 🎉`);
}
}

// Missing a suit
errorThrown("9");

// Missing a rank
errorThrown("♠");

// Invalid rank
errorThrown("1♠");
errorThrown("11♠");
errorThrown("Z♠");

// Invalid suit
errorThrown("9X");

// Suit and rank in the wrong order
errorThrown("♠9");

// Extra characters
errorThrown("9♠hello");
errorThrown("A♠♠");

// Spaces
errorThrown("9 ♠");
errorThrown(" 9♠");
errorThrown("9♠ ");

// Empty or incorrectly capitalised
errorThrown("");
errorThrown("a♠");
errorThrown("j♠");
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,28 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

// Case 2: Right angle
test('Should return "Right angle" when (angle ==90)', () => {
expect(getAngleType(90)).toEqual("Right angle");
});
// Case 3: Obtuse angles
test('Should return "Obtuse angle when (90<angle<180)', () => {
(expect(getAngleType(91)).toEqual, "Obtuse angle");
(expect(getAngleType(179)).toEqual, "Obtuse angle");
(expect(getAngleType(140)).toEqual, "Obtuse angle");
});
// Case 4: Straight angle
test('should return "Straight angle" when( angle ==180)', () => {
(expect(getAngleType(180)).toEqual, "Right angle");
});
// Case 5: Reflex angles
test('should return "Reflex angle" when (180< angle < 360);', () => {
(expect(getAngleType(250)).toEqual, "Reflex angle");
(expect(getAngleType(181)).toEqual, "Reflex angle");
(expect(getAngleType(359)).toEqual, "Reflex angle");
});
// Case 6: Invalid angles
test('should return "Invalid angle" when ( 0 < angle >360 || 0 == angle)', () => {
(expect(getAngleType(0)).toEqual, "Invalid angle");
(expect(getAngleType(400)).toEqual, "Invalid angle");
(expect(getAngleType(-4)).toEqual, "Invalid. angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.

// Special case: numerator is zero
test(`should return false when denominator is zero`, () => {
test(`should return FALSE when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});

test("should return FALSE when numerator is zero", () => {
expect(isProperFraction(0, 5)).toEqual(false);
});

test("should return TRUE when numerator is negative and smaller than denominator", () => {
expect(isProperFraction(-1, 7)).toEqual(true);
});

test("should return TRUE when denominator is negative AND bigger than numerator", () => {
expect(isProperFraction(1, -7)).toEqual(true);
});

test("should return FALSE when numerator > denominator ", () => {
expect(isProperFraction(9, 7)).toEqual(false);
});

test("should return TRUE when numerator < denominator ", () => {
expect(isProperFraction(4, 7)).toEqual(true);
});

test("should return TRUE when numerator AND denominator are negative values and the denominator is bigger than numerator", () => {
expect(isProperFraction(-4, -7)).toEqual(true);
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,40 @@ const getCardValue = require("../implement/3-get-card-value");

// Case 1: Ace (A)
test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♣")).toEqual(11);
expect(getCardValue("A♠")).toEqual(11);
expect(getCardValue("A♥")).toEqual(11);
expect(getCardValue("A♦")).toEqual(11);
});

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
// Face Cards (J, Q, K)
// Invalid Cards
// Case 2: Number Cards (2-10)
test(`Should return a value equal to the number on the card`, () => {
expect(getCardValue("2♠")).toEqual(2);
expect(getCardValue("5♥")).toEqual(5);
expect(getCardValue("9♦")).toEqual(9);
expect(getCardValue("10♣")).toEqual(10);
});

// Case 3: Face Cards (J, Q, K)
test(`Should return 10 when given a face card`, () => {
expect(getCardValue("K♥")).toEqual(10);
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("Q♦")).toEqual(10);
});

// Case 4: Invalid Cards
test(`All invalid cards should throw an Invalid error`, () => {
expect(() => getCardValue("9")).toThrow(/Invalid/); // missing suit
expect(() => getCardValue("♥")).toThrow(/Invalid/); // missing rank
expect(() => getCardValue("1♠")).toThrow(/Invalid/); // invalid rank
expect(() => getCardValue("11♠")).toThrow(/Invalid/); // rank too high
expect(() => getCardValue("9X")).toThrow(/Invalid/); // invalid suit
expect(() => getCardValue("♠9")).toThrow(/Invalid/); // wrong order
expect(() => getCardValue("9 ♠")).toThrow(/Invalid/); // space inside
expect(() => getCardValue("9♠ ")).toThrow(/Invalid/); // space after
expect(() => getCardValue("")).toThrow(/Invalid/); // empty string
expect(() => getCardValue("j♠")).toThrow(/Invalid/); // lowercase
});
// To learn how to test whether a function throws an error as expected in Jest,
// please refer to the Jest documentation:
// https://jestjs.io/docs/expect#tothrowerror

6 changes: 5 additions & 1 deletion Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
const regEx = new RegExp(`[^${findCharacter}]`, "g");
const numOfMatchedChar = stringOfCharacters.replaceAll(regEx, "").length;
return numOfMatchedChar;
}

console.log(countChar("dafsadf", "a"));

module.exports = countChar;
34 changes: 34 additions & 0 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,37 @@ test("should count multiple occurrences of a character", () => {
// And a character `char` that does not exist within `str`.
// When the function is called with these inputs,
// Then it should return 0, indicating that no occurrences of `char` were found.
test("should return 0 when no occurrence of 'char' is found in 'str'", () => {
const str = "cde";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(0);
});

// Scenario: Empty string
// Given an empty string `str`
// And a single character `char`
// When countChar is called
// Then it should return 0
test("should return 0 when the string is empty", () => {
const str = "";
const char = "a";

const count = countChar(str, char);

expect(count).toEqual(0);
});

// Scenario: One Occurrence
// Given a string `str`
// And a single character `char` that occurs once in `str`
// When countChar is called
// Then it should return 1
test("should count one occurrence of a character", () => {
const str = "abcde";
const char = "a";

const count = countChar(str, char);

expect(count).toEqual(1);
});
16 changes: 15 additions & 1 deletion Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
function getOrdinalNumber(num) {
return "1st";
const dictionary = {
1: "st",
2: "nd",
3: "rd",
};

const lastTwoDigit = num % 100;
const lastDigit = num % 10;

if (lastTwoDigit == 11 || lastTwoDigit == 12 || lastTwoDigit == 13) {
return `${num}` + `th`;
}

const ending = dictionary[lastDigit] || "th";
return `${num}${ending}`;
}

module.exports = getOrdinalNumber;
Loading
Loading