Skip to content
Closed
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 @@ -50,14 +50,16 @@ assertEquals(acute, "Acute angle");
// When the angle is greater than 90 degrees and less than 180 degrees,
// Then the function should return "Obtuse angle"
const obtuse = getAngleType(120);
// ====> write your test here, and then add a line to pass the test in the function above
assertEquals(obtuse, "Obtuse angle");

// Case 4: Identify Straight Angles:
// When the angle is exactly 180 degrees,
// Then the function should return "Straight angle"
// ====> write your test here, and then add a line to pass the test in the function above
const straight = getAngleType(180);
assertEquals(straight, "Straight angle");

// Case 5: Identify Reflex Angles:
// When the angle is greater than 180 degrees and less than 360 degrees,
// Then the function should return "Reflex angle"
// ====> write your test here, and then add a line to pass the test in the function above
const reflex = getAngleType(270);
assertEquals(reflex, "Reflex angle");
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,31 @@ assertEquals(improperFraction, false);
// target output: true
// Explanation: The fraction -4/7 is a proper fraction because the absolute value of the numerator (4) is less than the denominator (7). The function should return true.
const negativeFraction = isProperFraction(-4, 7);
// ====> complete with your assertion
assertEquals(negativeFraction, true);

// Equal Numerator and Denominator check:
// Input: numerator = 3, denominator = 3
// target output: false
// Explanation: The fraction 3/3 is not a proper fraction because the numerator is equal to the denominator. The function should return false.
const equalFraction = isProperFraction(3, 3);
// ====> complete with your assertion
assertEquals(equalFraction, false);

// Stretch:
// What other scenarios could you test for?


// // Stretch Scenarios:

// Stretch 1: Zero in numerator
// Input: numerator = 0, denominator = 5
// target output: true
// Explanation: The fraction 0/5 is a proper fraction because the numerator (0) is less than the denominator (5). Any fraction with zero numerator is proper (except when denominator is also zero).
const zeroNumerator = isProperFraction(0, 5);
assertEquals(zeroNumerator, true);

// Stretch 2: Negative denominator
// Input: numerator = 2, denominator = -5
// target output: false
// Explanation: The fraction 2/-5 equals -0.4, but when comparing 2 < -5, this is false. The function compares the actual values, not absolute values.
const negativeDenominator = isProperFraction(2, -5);
assertEquals(negativeDenominator, false);
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,23 @@
// write one test at a time, and make it pass, build your solution up methodically
// just make one change at a time -- don't rush -- programmers are deep and careful thinkers
function getCardValue(card) {
// Handle "10" case first since it has two characters
if (card.startsWith("10")) {
return 10;
}

const rank = card[0]; // Extract the rank from the card string
if (rank === "A") {
return 11;
}
if (rank >= "2" && rank <= "9") {
return parseInt(rank);
}
if (rank === "J" || rank === "Q" || rank === "K") {
return 10;
}

throw new Error("Invalid card rank.");
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -26,6 +40,7 @@ function assertEquals(actualOutput, targetOutput) {
`Expected ${actualOutput} to equal ${targetOutput}`
);
}

// Acceptance criteria:

// Given a card string in the format "A♠" (representing a card in blackjack - the last character will always be an emoji for a suit, and all characters before will be a number 2-10, or one letter of J, Q, K, A),
Expand All @@ -39,19 +54,38 @@ assertEquals(aceofSpades, 11);
// When the function is called with such a card,
// Then it should return the numeric value corresponding to the rank (e.g., "5" should return 5).
const fiveofHearts = getCardValue("5♥");
// ====> write your test here, and then add a line to pass the test in the function above
assertEquals(fiveofHearts, 5);

// Handle Face Cards (J, Q, K):
// Given a card with a rank of "10," "J," "Q," or "K",
// When the function is called with such a card,
// Then it should return the value 10, as these cards are worth 10 points each in blackjack.
const jackOfDiamonds = getCardValue("J♦");
assertEquals(jackOfDiamonds, 10);

const queenOfHearts = getCardValue("Q♥");
assertEquals(queenOfHearts, 10);

const kingOfClubs = getCardValue("K♣");
assertEquals(kingOfClubs, 10);

const tenOfClubs = getCardValue("10♣");
assertEquals(tenOfClubs, 10);

// Handle Ace (A):
// Given a card with a rank of "A",
// When the function is called with an Ace,
// Then it should, by default, assume the Ace is worth 11 points, which is a common rule in blackjack.
const aceOfDiamonds = getCardValue("A♦");
assertEquals(aceOfDiamonds, 11);

// Handle Invalid Cards:
// Given a card with an invalid rank (neither a number nor a recognized face card),
// When the function is called with such a card,
// Then it should throw an error indicating "Invalid card rank."
try {
getCardValue("X♠");
console.assert(false, "Expected an error for invalid card");
} catch (error) {
console.assert(error.message === "Invalid card rank.", "Expected invalid card rank error");
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,41 @@
// This statement loads the getAngleType function you wrote in the implement directory.
// We will use the same function, but write tests for it using Jest in this file.
const getAngleType = require("../implement/1-get-angle-type");
const getAngleType = require('../implement/1-get-angle-type');

test("should identify right angle (90°)", () => {
test("should identify right angle (90)", () => {
expect(getAngleType(90)).toEqual("Right angle");
});

// REPLACE the comments with the tests
// make your test descriptions as clear and readable as possible

// Case 2: Identify Acute Angles:
// When the angle is less than 90 degrees,
// Then the function should return "Acute angle"
test("should identify acute angles (less than 90 degrees)", () => {
expect(getAngleType(45)).toEqual("Acute angle");
expect(getAngleType(30)).toEqual("Acute angle");
expect(getAngleType(89)).toEqual("Acute angle");
});

// Case 3: Identify Obtuse Angles:
// When the angle is greater than 90 degrees and less than 180 degrees,
// Then the function should return "Obtuse angle"
test("should identify obtuse angles (between 90 and 180 degrees)", () => {
expect(getAngleType(120)).toEqual("Obtuse angle");
expect(getAngleType(91)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
});

// Case 4: Identify Straight Angles:
// When the angle is exactly 180 degrees,
// Then the function should return "Straight angle"
test("should identify straight angle (180 degrees)", () => {
expect(getAngleType(180)).toEqual("Straight angle");
});

// Case 5: Identify Reflex Angles:
// When the angle is greater than 180 degrees and less than 360 degrees,
// Then the function should return "Reflex angle"
test("should identify reflex angles (between 180 and 360 degrees)", () => {
expect(getAngleType(200)).toEqual("Reflex angle");
expect(getAngleType(181)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,20 @@ test("should return true for a proper fraction", () => {
});

// Case 2: Identify Improper Fractions:
test("should return false for improper fractions", () => {
expect(isProperFraction(5, 2)).toEqual(false);
expect(isProperFraction(10, 5)).toEqual(false);
});

// Case 3: Identify Negative Fractions:
test("should handle negative fractions correctly", () => {
expect(isProperFraction(-4, 7)).toEqual(true);
expect(isProperFraction(2, -5)).toEqual(false);
expect(isProperFraction(-2, -5)).toEqual(false);
});

// Case 4: Identify Equal Numerator and Denominator:
test("should return false when numerator equals denominator", () => {
expect(isProperFraction(3, 3)).toEqual(false);
expect(isProperFraction(7, 7)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@ test("should return 11 for Ace of Spades", () => {
});

// Case 2: Handle Number Cards (2-10):
test("should return numeric value for number cards (2-9)", () => {
expect(getCardValue('5♥')).toEqual(5);
expect(getCardValue('2♦')).toEqual(2);
expect(getCardValue('9♣')).toEqual(9);
});

// Case 3: Handle Face Cards (J, Q, K):
test("should return 10 for face cards (J, Q, K)", () => {
expect(getCardValue('J♦')).toEqual(10);
expect(getCardValue('Q♥')).toEqual(10);
expect(getCardValue('K♣')).toEqual(10);
});

// Case 4: Handle Ace (A):
test("should return 11 for all Aces", () => {
expect(getCardValue('A♦')).toEqual(11);
expect(getCardValue('A♥')).toEqual(11);
expect(getCardValue('A♣')).toEqual(11);
});

// Case 5: Handle Invalid Cards:
test("should throw error for invalid card ranks", () => {
expect(() => getCardValue('X♠')).toThrow('Invalid card rank.');
expect(() => getCardValue('1♥')).toThrow('Invalid card rank.');
});
50 changes: 46 additions & 4 deletions Sprint-3/2-practice-tdd/count.test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,66 @@
// implement a function countChar that counts the number of times a character occurs in a string
const countChar = require("./count");
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,
// Then it should:

// Scenario: Multiple Occurrences
// Scenario: Multiple occurrences
// Given the input string str,
// And a character char that may occur multiple times with overlaps within str (e.g., 'a' in 'aaaaa'),
// When the function is called with these inputs,
// Then it should correctly count overlapping occurrences of char (e.g., 'a' appears five times in 'aaaaa').

test("should count multiple occurrences of a character", () => {
const str = "aaaaa";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(5);
});

// Scenario: No Occurrences
// Scenario: No occurrences
// Given the input string str,
// And a character char that does not exist within the case-sensitive str,
// When the function is called with these inputs,
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
test("should return 0 when character is not found", () => {
const str = "hello world";
const char = "z";
const count = countChar(str, char);
expect(count).toEqual(0);
});

// Scenario: Case sensitivity
// Given the input string str with mixed case,
// And a character char with specific case,
// When the function is called with these inputs,
// Then it should only count occurrences that match the case exactly.
test("should be case sensitive when counting characters", () => {
const str = "Hello World";
const char = "h";
const count = countChar(str, char);
expect(count).toEqual(0); // 'h' is not the same as 'H'
});

// Scenario: Single occurrence
// Given the input string str,
// And a character char that appears exactly once,
// When the function is called with these inputs,
// Then it should return 1.
test("should count single occurrence correctly", () => {
const str = "hello";
const char = "e";
const count = countChar(str, char);
expect(count).toEqual(1);
});

// Scenario: Empty string
// Given an empty input string str,
// And any character char,
// When the function is called with these inputs,
// Then it should return 0.
test("should return 0 for empty string", () => {
const str = "";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(0);
});
67 changes: 64 additions & 3 deletions Sprint-3/2-practice-tdd/get-ordinal-number.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,74 @@
const getOrdinalNumber = require("./get-ordinal-number");
// In this week's prep, we started implementing getOrdinalNumber
const getOrdinalNumber = require('./get-ordinal-number');

// In this week's prep, we started implementing getOrdinalNumber
// continue testing and implementing getOrdinalNumber for additional cases
// Write your tests using Jest - remember to run your tests often for continual feedback

// Case 1: Identify the ordinal number for 1
// When the number is 1,
// Then the function should return "1st"

test("should return '1st' for 1", () => {
expect(getOrdinalNumber(1)).toEqual("1st");
});

// Case 2: Identify the ordinal number for 2
// When the number is 2,
// Then the function should return "2nd"
test("should return '2nd' for 2", () => {
expect(getOrdinalNumber(2)).toEqual("2nd");
});

// Case 3: Identify the ordinal number for 3
// When the number is 3,
// Then the function should return "3rd"
test("should return '3rd' for 3", () => {
expect(getOrdinalNumber(3)).toEqual("3rd");
});

// Case 4: Identify the ordinal number for numbers ending with 1 (except 11)
// When the number ends with 1 but is not 11,
// Then the function should return "[number]st"
test("should return correct ordinal for numbers ending with 1 (except 11)", () => {
expect(getOrdinalNumber(21)).toEqual("21st");
expect(getOrdinalNumber(31)).toEqual("31st");
expect(getOrdinalNumber(41)).toEqual("41st");
});

// Case 5: Identify the ordinal number for numbers ending with 2 (except 12)
// When the number ends with 2 but is not 12,
// Then the function should return "[number]nd"
test("should return correct ordinal for numbers ending with 2 (except 12)", () => {
expect(getOrdinalNumber(22)).toEqual("22nd");
expect(getOrdinalNumber(32)).toEqual("32nd");
expect(getOrdinalNumber(42)).toEqual("42nd");
});

// Case 6: Identify the ordinal number for numbers ending with 3 (except 13)
// When the number ends with 3 but is not 13,
// Then the function should return "[number]rd"
test("should return correct ordinal for numbers ending with 3 (except 13)", () => {
expect(getOrdinalNumber(23)).toEqual("23rd");
expect(getOrdinalNumber(33)).toEqual("33rd");
expect(getOrdinalNumber(43)).toEqual("43rd");
});

// Case 7: Identify the ordinal number for numbers ending with 11, 12, 13
// When the number is 11, 12, or 13,
// Then the function should return "[number]th"
test("should return 'th' for numbers 11, 12, 13", () => {
expect(getOrdinalNumber(11)).toEqual("11th");
expect(getOrdinalNumber(12)).toEqual("12th");
expect(getOrdinalNumber(13)).toEqual("13th");
});

// Case 8: Identify the ordinal number for other numbers
// When the number doesn't end with 1, 2, or 3 (or is 11, 12, 13),
// Then the function should return "[number]th"
test("should return 'th' for other numbers", () => {
expect(getOrdinalNumber(4)).toEqual("4th");
expect(getOrdinalNumber(5)).toEqual("5th");
expect(getOrdinalNumber(10)).toEqual("10th");
expect(getOrdinalNumber(15)).toEqual("15th");
expect(getOrdinalNumber(20)).toEqual("20th");
expect(getOrdinalNumber(100)).toEqual("100th");
});
Loading
Loading