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
19 changes: 12 additions & 7 deletions Sprint-3/1-key-implement/1-get-angle-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
// Then, write the next test! :) Go through this process until all the cases are implemented

function getAngleType(angle) {
if (angle === 90) return "Right angle";
// read to the end, complete line 36, then pass your test here
if (angle === 90) return "Right angle";
if (angle > 0 && angle < 90) return "Acute angle";
if (angle > 90 && angle < 180) return "Obtuse angle";
if (angle === 180) return "Straight angle";
if (angle > 180 && angle < 360) return "Reflex angle";
}

// we're going to use this helper function to make our assertions easier to read
Expand All @@ -36,21 +39,23 @@ assertEquals(right, "Right angle");
// Case 2: Identify Acute Angles:
// When the angle is less than 90 degrees,
// Then the function should return "Acute angle"
const acute = getAngleType(45);
const acute = getAngleType(1);
assertEquals(acute, "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"
const obtuse = getAngleType(120);
// ====> write your test here, and then add a line to pass the test in the function above
const obtuse = getAngleType(91);
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(181);
assertEquals(reflex, "Reflex angle");
48 changes: 39 additions & 9 deletions Sprint-3/1-key-implement/2-is-proper-fraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
// write one test at a time, and make it pass, build your solution up methodically

function isProperFraction(numerator, denominator) {
if (numerator < denominator) return true;
// ensure that the both inputs are evaluated to positive numbers by using Math.abs(), eg -4 returns 4, 4 returns 4, and 0 returns 0
return Math.abs(numerator) < Math.abs(denominator); // evaluates to either true or false
}

// here's our helper again
Expand All @@ -25,29 +26,58 @@ function assertEquals(actualOutput, targetOutput) {
// Input: numerator = 2, denominator = 3
// target output: true
// Explanation: The fraction 2/3 is a proper fraction, where the numerator is less than the denominator. The function should return true.
const properFraction = isProperFraction(2, 3);
assertEquals(properFraction, true);
// const properFraction = isProperFraction(2, 3);
// assertEquals(properFraction, true);

// Improper Fraction check:
// Input: numerator = 5, denominator = 2
// target output: false
// Explanation: The fraction 5/2 is an improper fraction, where the numerator is greater than or equal to the denominator. The function should return false.
const improperFraction = isProperFraction(5, 2);
assertEquals(improperFraction, false);
// const improperFraction = isProperFraction(5, 2);
// assertEquals(improperFraction, false);

// Negative Fraction check:
// Input: numerator = -4, denominator = 7
// 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
// const negativeFraction = isProperFraction(-4, 7);
// 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
// const equalFraction = isProperFraction(3, 3);
// assertEquals(equalFraction, false);

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

/*
Using an array to make code DRY and easier to review, using the format [numerator, denominator, expected] that will be used in the updated helper function
*/
const testCases = [
// original tests from earlier in file
[2, 3, true],
[5, 2, false],
[-4, 7, true],
[3, 3, false],

// additional tests
[1, 0, false], //zero denominator input
["2", "3", true], //string inputs
[2.5, 3.1, true], //decimal number inputs
[true, false, false], //boolean inputs
[null, 3, true], //null numerator input, (N.B. Math.abs(null) is 0, therefore this is the equivalent of evaluating to [0, 3, true])
[2, null, false], //null denominator input, (N.B. Math.abs(null) is 0, therefore this is the equivalent of evaluating to [2, 0, false])
[NaN, 3, false], //Nan numerator input
[2, NaN, false], //Nan denominator input
[undefined, 3, false], //undefined numerator input
[2, undefined, false], //undefined denominator input
];

// adjust helper function to loop through the array values, and assigns the array values to numerator, denominator, expected, eg, [2, 3, True].
for (const [numerator, denominator, expected] of testCases) {
const result = isProperFraction(numerator, denominator);
assertEquals(result, expected);
}
46 changes: 44 additions & 2 deletions Sprint-3/1-key-implement/3-get-card-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,22 @@
// 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) {
if (rank === "A") return 11;
// use an array to hold the values of the face cards.
const faceCards = ["J", "Q", "K"];

/*
use slice to extract the rank (the numeric or face card value) from the "card" value. Start at the beginning index of the string (0) and go up to but not including the last character (-1 means one from the end)
*/
const rank = card.slice(0, -1);

if (rank === "A") return 11; //handles Ace cards
if (rank >= 2 && rank <= 10) return Number(rank); //handles cards 2-10

// use the includes() method to check if the value of the variable rank is one of the faceCards array values.
if (faceCards.includes(rank)) return 10; //handles face cards

// use the built-in JS Error() object to throw an error with a message if the value of "card" is invalid (with the message as the argument)
throw new Error("invalid card rank"); //handles all invalid "card" values
}

// You need to write assertions for your function to check it works in different cases
Expand All @@ -33,19 +48,46 @@ 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 jackofClubs = getCardValue("J♣");
assertEquals(jackofClubs, 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 aceCard = getCardValue("A♦");
assertEquals(aceCard, 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."
const invalidCards = [
["G♥", "invalid card rank"], // random rank letter
["1♠", "invalid card rank"], // 1 is not a valid card
["11♦", "invalid card rank"], // 11 is not a valid card
["62♣", "invalid card rank"], //random rank number
["", "invalid card rank"], //empty string
["NaN♠", "invalid card rank"], //NaN as rank
["undefined♣", "invalid card rank"], //undefined as rank
["null♦", "invalid card rank"], // null as rank
];

/*
use a try block to call the getCardValue function, if it does not throw an error it checks the returned value against the expeced result. However if the function does throw an error it catches the error and checks if the error message matches "invalid card rank". In the catch block, the error.message argument in the assertEquals() function refers to Error("invalid card rank") object in the getCardValue() function at the top of the code
*/
for (const [input, expected] of invalidCards) {
try {
const result = getCardValue(input);
assertEquals(result, expected);
} catch (error) {
assertEquals(error.message, expected);
}
}
17 changes: 6 additions & 11 deletions Sprint-3/2-mandatory-rewrite/1-get-angle-type.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
function getAngleType(angle) {
if (angle === 90) return "Right angle";
// replace with your completed function from key-implement

if (angle > 0 && angle < 90) return "Acute angle";
if (angle > 90 && angle < 180) return "Obtuse angle";
if (angle === 180) return "Straight angle";
if (angle > 180 && angle < 360) return "Reflex angle";
}








// Don't get bogged down in this detail
// Jest uses CommonJS module syntax by default as it's quite old
// We will upgrade our approach to ES6 modules in the next course module, so for now
// We will upgrade our approach to ES6 modules in the next course module, so for now
// we have just written the CommonJS module.exports syntax for you
module.exports = getAngleType;
module.exports = getAngleType;
20 changes: 12 additions & 8 deletions Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@ test("should identify right angle (90°)", () => {
// 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 an acute angle (less than 90°)", () => {
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 an obtuse angle (greater than 90°)", () => {
expect(getAngleType(91)).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 an straight angle (exactly 180°)", () => {
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 a reflex angle (greater than 180° but less than 360°)", () => {
expect(getAngleType(181)).toEqual("Reflex angle");
});
7 changes: 4 additions & 3 deletions Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
function isProperFraction(numerator, denominator) {
if (numerator < denominator) return true;
// add your completed function from key-implement here
// ensure that the both inputs are evaluated to positive numbers by using Math.abs(), eg -4 returns 4, 4 returns 4, and 0 returns 0
if (Math.abs(numerator) < Math.abs(denominator)) return true;
return false; //if the inputs don't satisfy the expression it returns false
}

module.exports = isProperFraction;
module.exports = isProperFraction;
9 changes: 9 additions & 0 deletions Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@ test("should return true for a proper fraction", () => {
});

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

Choose a reason for hiding this comment

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

Jest also has .toBeTruthy(): https://jestjs.io/docs/expect#tobetruthy

Why might that not be a good thing to use here?

Copy link
Author

Choose a reason for hiding this comment

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

I imagine it's not great to use at this test probably because if something is "truthy" it is less strict.. or something to that effect? I would need to make sure that an improper fraction was handled strictly to get the exact correct values.

Choose a reason for hiding this comment

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

That is exactly it - truthy values are a lot more than true which means it is less strict than .toEqual(true). It's any value that would be coerced into true when in a boolean context

https://developer.mozilla.org/en-US/docs/Glossary/Truthy

Boolean("hello") // true
Boolean([]) // true
Boolean({}) // true

});

// Case 3: Identify Negative Fractions:
test("should return true for a negative fraction", () => {
expect(isProperFraction(-4, -7)).toEqual(true);
});

// Case 4: Identify Equal Numerator and Denominator:
test("should return true for an equal numerator and denominator", () => {
expect(isProperFraction(3, 3)).toEqual(false);
});
21 changes: 18 additions & 3 deletions Sprint-3/2-mandatory-rewrite/3-get-card-value.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
function getCardValue(card) {
// replace with your code from key-implement
return 11;
// use an array to hold the values of the face cards.
const faceCards = ["10", "J", "Q", "K"];

/*
use slice to extract the rank (the numeric or face card value) from the "card" value. Start at the beginning index of the string (0) and go up to but not including the last character (-1 means one from the end)
*/
const rank = card.slice(0, -1);

if (rank === "A") return 11; //handles Ace cards
if (Number(rank) >= 2 && Number(rank) < 10) return Number(rank); //handles cards 2-9

// use the includes() method to check if the value of the variable rank is one of the faceCards array values.
if (faceCards.includes(rank)) return 10; //handles face cards

// use the built-in JS Error() object to throw an error with a message if the value of "card" is invalid (with the message as the argument)
throw new Error("Invalid card rank"); //handles all invalid "card" values
}
module.exports = getCardValue;

module.exports = getCardValue;
53 changes: 49 additions & 4 deletions Sprint-3/2-mandatory-rewrite/3-get-card-value.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,56 @@
const getCardValue = require("./3-get-card-value");

test("should return 11 for Ace of Spades", () => {
const aceofSpades = getCardValue("A♠");
expect(aceofSpades).toEqual(11);
});
const aceofSpades = getCardValue("A♠");
expect(aceofSpades).toEqual(11);
});

// Case 2: Handle Number Cards (2-10):
test("should return correlating number for cards with ranks between 2-10", () => {
const numberCard = getCardValue("5♥");
expect(numberCard).toEqual(5);
});
// Case 3: Handle Face Cards (J, Q, K):
test("should return 10 for face cards of J, Q, K", () => {
const faceCard = getCardValue("J♣");
expect(faceCard).toEqual(10);
});
// Case 4: Handle Ace (A):
// Case 5: Handle Invalid Cards:
test("should return 11 for Ace of Spades", () => {
const aceCard = getCardValue("A♦");
expect(aceCard).toEqual(11);
});
// Case 5: Handle Invalid Cards - using an array for the inputs :
// test("should throw an error for invalid card ranks", () => {
// const invalidCardInput = [
// "G♥", // random rank letter
// "1♠", // 1 is not a valid card
// "11♦", // 11 is not a valid card
// "62♣", //random rank number
// "", //empty string
// "NaN♠", //NaN as rank
// "undefined♣", //undefined as rank
// "null♦", // null as rank
// ];

// I have learned that we can use Jest's .each() for clearer test output, better reporting, parallel execution (improves test performance) and less boilerplate code. in the description of the test we can use placeholders such as %s (string value) and %d (number , or digit, value) as placeholders for the actual test cases inputs, meaning that when the error message appears we can immediately know which case it refers to. E.g if the current input is "G♥", the test name will be: should throw an error for invalid card rank: G♥"
test.each([
"G♥", // random rank letter
"1♠", // 1 is not a valid card
"11♦", // 11 is not a valid card
"62♣", // random rank number
"", // empty string
"NaN♠", // NaN as rank
"undefined♣", // undefined as rank
"null♦", // null as rank
])("should throw an error for invalid card rank: %s", (input) => {
expect(() => getCardValue(input)).toThrow("Invalid card rank");
});

for (const input of invalidCardInput) {
expect(() => getCardValue(input)).toThrow("Invalid card rank");
/*
Jest doesn;t test the value itself, it tests whether the function is throwing an error. If getCardValue(input) is called directly (i.e. using: expect(getCardValue(input)).toThrow("Invalid card rank") instead), the error happens immediately and Jest never gets a chance to check it, therefore the test fails. Wrapping getCardValue(input) in an anonymous function ensures that it delays the function execution allowing Jest to observe the error instead of triggering it too soon.
*/
}
});
11 changes: 9 additions & 2 deletions Sprint-3/3-mandatory-practice/implement/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
let count = 0;
for (const char of stringOfCharacters) {
//loops through each character in the string input
if (char === findCharacter)
//checks if the character matches the findCharacter input
count++; //increases the count when a match is found in the input
}
return count; //return the actual count of the characters that match
}

module.exports = countChar;
module.exports = countChar;
6 changes: 6 additions & 0 deletions Sprint-3/3-mandatory-practice/implement/count.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ test("should count multiple occurrences of a character", () => {
// 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 count no occurrences of a character", () => {
const str = "aaaaa";
const char = "z";
const count = countChar(str, char);
expect(count).toEqual(0);
});
Loading