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 @@ -9,25 +9,12 @@
// Acceptance criteria:
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
}
if (denominator === 0) {
return false;
}

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

// Here's our helper again
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
return Math.abs(numerator) < Math.abs(denominator);
}

// TODO: Write tests to cover all cases.
// What combinations of numerators and denominators should you test?

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
module.exports = isProperFraction;
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,27 @@
// Acceptance criteria:
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
}
const rank = card.slice(0, -1);
const suit = card.slice(-1);

// The line below allows us to load the getCardValue function into tests in other files.
// This will be useful in the "rewrite tests with jest" step.
module.exports = getCardValue;
const validRanks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];

// Helper functions to make our assertions easier to read.
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}
const validSuits = ["♠", "♥", "♦", "♣"];

if (!validRanks.includes(rank) || !validSuits.includes(suit)) {
throw new Error("Invalid card");
}

// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:
assertEquals(getCardValue("9♠"), 9);
if (rank === "A") {
return 11;
}

// Handling invalid cards
try {
getCardValue("invalid");
if (["J", "Q", "K"].includes(rank)) {
return 10;
}

// This line will not be reached if an error is thrown as expected
console.error("Error was not thrown for invalid card 😢");
} catch (e) {
console.log("Error thrown for invalid card 🎉");
return Number(rank);
}

// What other invalid card cases can you think of?
module.exports = getCardValue;
4 changes: 2 additions & 2 deletions Sprint-3/2-practice-tdd/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
return stringOfCharacters.split("").filter(char => char === findCharacter).length;
}

module.exports = countChar;
module.exports = countChar;
8 changes: 7 additions & 1 deletion Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// implement a function countChar that counts the number of times a character occurs in a string
// implement a function countChar that counts the number of times a character occurs in a string
const countChar = require("./count");
// Given a string `str` and a single character `char` to search for,
// When the countChar function is called with these inputs,
Expand All @@ -9,7 +10,6 @@ const countChar = require("./count");
// And a character `char` that occurs one or more times in `str` (e.g., 'a' in 'aaaaa'),
// When the function is called with these inputs,
// Then it should correctly count occurrences of `char`.

test("should count multiple occurrences of a character", () => {
const str = "aaaaa";
const char = "a";
Expand All @@ -22,3 +22,9 @@ 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 the character does not occur", () => {
const str = "aaaaa";
const char = "b";
const count = countChar(str, char);
expect(count).toEqual(0);
});
19 changes: 17 additions & 2 deletions Sprint-3/2-practice-tdd/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
function getOrdinalNumber(num) {
return "1st";
const lastTwoDigits = num % 100;
const lastDigit = num % 10;

if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
return `${num}th`;
}

if (lastDigit === 1) {
return `${num}st`;
} else if (lastDigit === 2) {
return `${num}nd`;
} else if (lastDigit === 3) {
return `${num}rd`;
} else {
return `${num}th`;
}
}

module.exports = getOrdinalNumber;
module.exports = getOrdinalNumber;
14 changes: 13 additions & 1 deletion Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ const getOrdinalNumber = require("./get-ordinal-number");
// Then the function should return a string by appending "st" to the number.
test("should append 'st' for numbers ending with 1, except those ending with 11", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(31)).toEqual("31st");
expect(getOrdinalNumber(131)).toEqual("131st");
});
test(`should append 'nd' for numbers ending with 2 except those ending with 12 `, () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(132)).toEqual("132nd");
});

test(`should append 'rd' for numbers ending with 3 except those ending with 13 `, () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
expect(getOrdinalNumber(23)).toEqual("23rd");
expect(getOrdinalNumber(143)).toEqual("143rd");
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider adding more test cases regarding the edge case th i.e 11th, 12th etc