diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js b/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js index 970cb9b64..c5b8f7edb 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js @@ -10,24 +10,52 @@ // 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) { +//function isProperFraction(numerator, denominator) { // TODO: Implement this function -} +//} // 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; +//module.exports = isProperFraction; // Here's our helper again -function assertEquals(actualOutput, targetOutput) { +/*function assertEquals(actualOutput, targetOutput) { console.assert( actualOutput === targetOutput, `Expected ${actualOutput} to equal ${targetOutput}` - ); -} + );*/ +//} // 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); +function isProperFraction(numerator, denominator) { + // Return non numeric numerators and denominators as false + if (typeof numerator != "number" || typeof denominator != "number") + return false; + + return Math.abs(numerator) < Math.abs(denominator); +} + +module.exports = isProperFraction; + +function assertEquals(actualOutput, targetOutput) { + console.assert( + actualOutput === targetOutput, + `Expected ${actualOutput} to equal ${targetOutput}` + ); +} + +// Positive proper fraction test assertEquals(isProperFraction(1, 2), true); + +// Improper proper fraction test +assertEquals(isProperFraction(3, 2), false); + +// Negative proper fraction test +assertEquals(isProperFraction(-1, 2), true); + +// Zero numerator test +assertEquals(isProperFraction(0, 2), true); diff --git a/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js b/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js index c7559e787..fb34bf4d5 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js +++ b/Sprint-3/1-implement-and-rewrite-tests/implement/3-get-card-value.js @@ -21,32 +21,77 @@ // 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) { +/*function getCardValue(card) { // TODO: Implement this function -} +}*/ // 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; +//module.exports = getCardValue; // Helper functions to make our assertions easier to read. -function assertEquals(actualOutput, targetOutput) { +/*function assertEquals(actualOutput, targetOutput) { console.assert( actualOutput === targetOutput, `Expected ${actualOutput} to equal ${targetOutput}` ); -} +}*/ // TODO: Write tests to cover all outcomes, including throwing errors for invalid cards. -// Examples: -assertEquals(getCardValue("9♠"), 9); +// Examples:function getCardValue(card) { + // Ensure that the last char is a suit, otherwise throw an error + function getCardValue(card) { + // Type check + if (typeof card !== "string") { + throw new Error("Invalid card"); + } + + const suits = ["♠", "♥", "♦", "♣"]; + if (!suits.includes(card.slice(-1))) { + throw new Error("Invalid card"); + } + + const rank = card.slice(0, -1); + const validRank = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]; + + if (!validRank.includes(rank)) { + throw new Error("Invalid card"); + } + + // Clean lookup for values + const rankValues = { + "A": 11, "2": 2, "3": 3, "4": 4, "5": 5, "6": 6, "7": 7, "8": 8, "9": 9, "10": 10, + "J": 10, "Q": 10, "K": 10 + }; + + return rankValues[rank]; +} + +module.exports = getCardValue; + + +function assertEquals(actualOutput, targetOutput) { + console.assert( + actualOutput === targetOutput, + `Expected ${actualOutput} to equal ${targetOutput}` + ); +} -// Handling invalid cards -try { - getCardValue("invalid"); +// Tests +assertEquals(getCardValue("A♠"), 11); +assertEquals(getCardValue("K♥"), 10); +assertEquals(getCardValue("10♦"), 10); +assertEquals(getCardValue("2♣"), 2); - // This line will not be reached if an error is thrown as expected - console.error("Error was not thrown for invalid card"); -} catch (e) {} +// Invalid card tests +const invalidCards = ["invalid", 7, "", "AA♠", "10♠♦", "10"]; +invalidCards.forEach(invalidCard => { + try { + getCardValue(invalidCard); + console.error(`❌ Error not thrown for: ${invalidCard}`); + } catch (e) { + // Expected + } +}); -// What other invalid card cases can you think of? +console.log("\n✅ All tests completed!"); \ No newline at end of file diff --git a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js index d777f348d..eb69642a4 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js +++ b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/1-get-angle-type.test.js @@ -1,20 +1,43 @@ -// 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"); -// TODO: Write tests in Jest syntax to cover all cases/outcomes, -// including boundary and invalid cases. - // Case 1: Acute angles test(`should return "Acute angle" when (0 < angle < 90)`, () => { - // Test various acute angles, including boundary cases expect(getAngleType(1)).toEqual("Acute angle"); expect(getAngleType(45)).toEqual("Acute angle"); expect(getAngleType(89)).toEqual("Acute angle"); }); // 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.0)).toEqual("Obtuse angle"); + expect(getAngleType(100)).toEqual("Obtuse angle"); + expect(getAngleType(179)).toEqual("Obtuse angle"); +}); + // Case 4: Straight angle +test(`should return "Straight angle" when (angle === 180)`, () => { + expect(getAngleType(180)).toEqual("Straight angle"); +}); + // Case 5: Reflex angles +test(`should return "Reflex angle" when (180 < angle < 360)`, () => { + expect(getAngleType(181)).toEqual("Reflex angle"); + expect(getAngleType(200)).toEqual("Reflex angle"); + expect(getAngleType(359)).toEqual("Reflex angle"); +}); + // Case 6: Invalid angles +test(`should return "Invalid angle" when angle doesn't lie between 0-360 exclusive`, () => { + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType(360)).toEqual("Invalid angle"); + expect(getAngleType(500)).toEqual("Invalid angle"); + expect(getAngleType(-200)).toEqual("Invalid angle"); + expect(getAngleType("")).toEqual("Invalid angle"); + expect(getAngleType("ten degrees")).toEqual("Invalid angle"); + expect(getAngleType(true)).toEqual("Invalid angle"); +}); \ No newline at end of file diff --git a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js index 7f087b2ba..3bd48190e 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js +++ b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/2-is-proper-fraction.test.js @@ -1,10 +1,51 @@ // This statement loads the isProperFraction function you wrote in the implement directory. // We will use the same function, but write tests for it using Jest in this file. -const isProperFraction = require("../implement/2-is-proper-fraction"); +//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`, () => { - expect(isProperFraction(1, 0)).toEqual(false); + +const getAngleType = require("../implement/1-get-angle-type"); + +// Case 1: Acute angles +test(`should return "Acute angle" when (0 < angle < 90)`, () => { + expect(getAngleType(1)).toEqual("Acute angle"); + expect(getAngleType(45)).toEqual("Acute angle"); + expect(getAngleType(89)).toEqual("Acute angle"); +}); + +// 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.0)).toEqual("Obtuse angle"); + expect(getAngleType(100)).toEqual("Obtuse angle"); + expect(getAngleType(179)).toEqual("Obtuse angle"); +}); + +// Case 4: Straight angle +test(`should return "Straight angle" when (angle === 180)`, () => { + expect(getAngleType(180)).toEqual("Straight angle"); +}); + +// Case 5: Reflex angles +test(`should return "Reflex angle" when (180 < angle < 360)`, () => { + expect(getAngleType(181)).toEqual("Reflex angle"); + expect(getAngleType(200)).toEqual("Reflex angle"); + expect(getAngleType(359)).toEqual("Reflex angle"); +}); + +// Case 6: Invalid angles +test(`should return "Invalid angle" when angle doesn't lie between 0-360 exclusive`, () => { + expect(getAngleType(0)).toEqual("Invalid angle"); + expect(getAngleType(360)).toEqual("Invalid angle"); + expect(getAngleType(500)).toEqual("Invalid angle"); + expect(getAngleType(-200)).toEqual("Invalid angle"); + expect(getAngleType("")).toEqual("Invalid angle"); + expect(getAngleType("ten degrees")).toEqual("Invalid angle"); + expect(getAngleType(true)).toEqual("Invalid angle"); }); +// ... rest of isProperFraction tests \ No newline at end of file diff --git a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js index cf7f9dae2..6d998be29 100644 --- a/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js +++ b/Sprint-3/1-implement-and-rewrite-tests/rewrite-tests-with-jest/3-get-card-value.test.js @@ -1,13 +1,12 @@ // This statement loads the getCardValue function you wrote in the implement directory. // We will use the same function, but write tests for it using Jest in this file. -const getCardValue = require("../implement/3-get-card-value"); +//const getCardValue = require("../implement/3-get-card-value"); // TODO: Write tests in Jest syntax to cover all possible outcomes. // Case 1: Ace (A) -test(`Should return 11 when given an ace card`, () => { - expect(getCardValue("A♠")).toEqual(11); -}); +//test(`Should return 11 when given an ace card`, () => { + //expect(getCardValue("A♠")).toEqual(11);}); // Suggestion: Group the remaining test data into these categories: // Number Cards (2-10) @@ -17,4 +16,45 @@ test(`Should return 11 when given an ace card`, () => { // 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 +const getCardValue = require("../implement/3-get-card-value"); +const suits = ["♠", "♥", "♦", "♣"]; + +// Case 1: Ace (A) +test(`Should return 11 when given an ace card`, () => { + for (const suit of suits) { + const ace = `A${suit}`; + expect(getCardValue(ace)).toEqual(11); + } +}); + +// Case 2: Face Cards (J, Q, K) +test(`Should return 10 when given a face card`, () => { + const faceCards = ["J", "Q", "K"]; + for (const faceCard of faceCards) { + for (const suit of suits) { + expect(getCardValue(`${faceCard}${suit}`)).toEqual(10); + } + } +}); + +// Case 3: Number Cards (2-10) +test(`Should return the numerical value of number cards`, () => { + const cardsNumbers = [2, 3, 4, 5, 6, 7, 8, 9, 10]; + for (const rank of cardsNumbers) { + for (const suit of suits) { + const cardFace = `${rank}${suit}`; + expect(getCardValue(cardFace)).toEqual(rank); + } + } +}); + +// Case 4: Invalid Cards +test(`Should throw an error when given an invalid card`, () => { + const invalidCards = ["invalid", 7, "", "AA♠", "10♠♦"]; + for (const invalidCard of invalidCards) { + expect(() => { + getCardValue(invalidCard); + }).toThrow(); + } +}); diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..29b41b5a6 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,9 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + for (let i = 0; i < stringOfCharacters.length; i++) { + if (stringOfCharacters[i] === findCharacter) { + } + } + return count; } - -module.exports = countChar; +module.exports = countChar; \ No newline at end of file