Skip to content

ITPJAN|SARAAMIRI|Module-structuring-and-testing-data|sprint3|week3 #359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
24 changes: 23 additions & 1 deletion Sprint-3/1-key-implement/1-get-angle-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,23 @@
function getAngleType(angle) {
if (angle === 90) return "Right angle";
// read to the end, complete line 36, then pass your test here

// Case 2: Acute angle (less than 90 degrees)
if (angle < 90) return "Acute angle";

// Case 3: Obtuse angle (greater than 90 but less than 180 degrees)
if (angle > 90 && angle < 180) return "Obtuse angle";

// Case 4: Straight angle (exactly 180 degrees)
if (angle === 180) return "Straight angle";

// Case 5: Reflex angle (greater than 180 but less than 360 degrees)
if (angle > 180 && angle < 360) return "Reflex angle";
}




// we're going to use this helper function to make our assertions easier to read
// if the actual output matches the target output, the test will pass
function assertEquals(actualOutput, targetOutput) {
Expand Down Expand Up @@ -53,4 +68,11 @@ const obtuse = getAngleType(120);
// 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
// ====> write your test here, and then add a line to pass the test in the function above

// Now we run the tests:
assertEquals(getAngleType(90), "Right angle"); // Expected to pass
assertEquals(getAngleType(45), "Acute angle"); // Expected to pass
assertEquals(getAngleType(120), "Obtuse angle"); // Expected to pass
assertEquals(getAngleType(180), "Straight angle"); // Expected to pass
assertEquals(getAngleType(270), "Reflex angle"); // Expected to pass
13 changes: 12 additions & 1 deletion Sprint-3/1-key-implement/2-is-proper-fraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@
// write one test at a time, and make it pass, build your solution up methodically

function isProperFraction(numerator, denominator) {
if (numerator < denominator) return true;
// Handle cases where the denominator is zero or less
if (denominator <= 0) {
throw new Error('Denominator must be greater than zero');
}

// Proper fraction if absolute value of numerator is less than denominator
return Math.abs(numerator) < Math.abs(denominator);
}


// here's our helper again
function assertEquals(actualOutput, targetOutput) {
console.assert(
Expand Down Expand Up @@ -40,13 +47,17 @@ 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);
assertEquals(negativeFraction, true);

// ====> complete with your assertion

// 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);
assertEquals(equalFraction, false);

// ====> complete with your assertion

// Stretch:
Expand Down
42 changes: 37 additions & 5 deletions Sprint-3/1-key-implement/3-get-card-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
// complete the rest of the tests and cases
// 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;
}

// You need to write assertions for your function to check it works in different cases
// we're going to use this helper function to make our assertions easier to read
Expand All @@ -24,8 +21,7 @@ function assertEquals(actualOutput, targetOutput) {

// 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),
// When the function getCardValue is called with this card string as input,
// Then it should return the numerical card value
const aceofSpades = getCardValue("A♠");
// Then it should return the numerical card valueconst aceofSpades = getCardValue("A♠");
assertEquals(aceofSpades, 11);

// Handle Number Cards (2-10):
Expand All @@ -49,3 +45,39 @@ const fiveofHearts = getCardValue("5♥");
// 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."

function getCardValue(card) {
const rank = card.slice(0, -1); // Extract the rank (everything except the suit emoji)

if (rank === "A") return 11;
if (rank === "J" || rank === "Q" || rank === "K" || rank === "10") return 10;
if (parseInt(rank) >= 2 && parseInt(rank) <= 9) return parseInt(rank);

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


// Test for Ace
const aceofSpades = getCardValue("A♠");
assertEquals(aceofSpades, 11);

// Test for number cards
const fiveofHearts = getCardValue("5♥");
assertEquals(fiveofHearts, 5);

// Test for face cards (J, Q, K)
const jackOfSpades = getCardValue("J♠");
assertEquals(jackOfSpades, 10);

const queenOfSpades = getCardValue("Q♠");
assertEquals(queenOfSpades, 10);

const kingOfSpades = getCardValue("K♠");
assertEquals(kingOfSpades, 10);

// Test for invalid card
try {
getCardValue("X♠");
Copy link
Contributor

Choose a reason for hiding this comment

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

if GetCardValue() didn't throw an exception, and returned a value like 10 for example, wouldn't the test still pass?

} catch (error) {
assertEquals(error.message, "Invalid card rank");
}
10 changes: 10 additions & 0 deletions Sprint-3/2-mandatory-rewrite/1-get-angle-type.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
function getAngleType(angle) {
if (angle === 90) return "Right angle";
// replace with your completed function from key-implement
// Case 2: Acute angle (less than 90 degrees)
if (angle < 90) return "Acute angle";

// Case 3: Obtuse angle (greater than 90 but less than 180 degrees)
if (angle > 90 && angle < 180) return "Obtuse angle";

// Case 4: Straight angle (exactly 180 degrees)
if (angle === 180) return "Straight angle";

// Case 5: Reflex angle (greater than 180 but less than 360 degrees)
if (angle > 180 && angle < 360) return "Reflex angle";
}


Expand Down
13 changes: 13 additions & 0 deletions Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,28 @@ test("should identify right angle (90°)", () => {
// Case 2: Identify Acute Angles:
// When the angle is less than 90 degrees,
// Then the function should return "Acute angle"
test("should identify acute angle (less than 90°)", () => {
expect(getAngleType(45)).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 angle (greater than 90° but less than 180°)", () => {
expect(getAngleType(120)).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°)", () => {
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 angle (greater than 180° but less than 360°)", () => {
expect(getAngleType(250)).toEqual("Reflex angle");
});
21 changes: 16 additions & 5 deletions Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
function isProperFraction(numerator, denominator) {
if (numerator < denominator) return true;
// add your completed function from key-implement here
}

module.exports = isProperFraction;
// Handle case where the denominator is zero
if (denominator === 0) {
throw new Error('Denominator cannot be zero');
}

// A proper fraction is one where the absolute value of the numerator is less than the denominator
if (Math.abs(numerator) < Math.abs(denominator)) {
return true;
}

// Otherwise, it's an improper fraction
return false;
}

module.exports = isProperFraction;

17 changes: 11 additions & 6 deletions Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
const isProperFraction = require("./2-is-proper-fraction");

test("should return true for a proper fraction", () => {
expect(isProperFraction(2, 3)).toEqual(true);
test("Identifies Proper Fraction", () => {
expect(isProperFraction(2, 3)).toBe(true);
});

// Case 2: Identify Improper Fractions:
test("Identifies Improper Fraction", () => {
expect(isProperFraction(5, 2)).toBe(false);
});

// Case 3: Identify Negative Fractions:
test("Identifies Negative Proper Fraction", () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

should also have a test for negative improper fraction.

expect(isProperFraction(-4, 7)).toBe(true);
});

// Case 4: Identify Equal Numerator and Denominator:
test("Identifies Equal Numerator and Denominator as Improper", () => {
expect(isProperFraction(3, 3)).toBe(false);
});
13 changes: 10 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,12 @@
function getCardValue(card) {
// replace with your code from key-implement
return 11;
}

const rank = card.slice(0, -1); // Extract the rank (everything except the suit emoji)

if (rank === "A") return 11;
if (rank === "J" || rank === "Q" || rank === "K" || rank === "10") return 10;
if (parseInt(rank) >= 2 && parseInt(rank) <= 9) return parseInt(rank);

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

module.exports = getCardValue;
28 changes: 23 additions & 5 deletions Sprint-3/2-mandatory-rewrite/3-get-card-value.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,27 @@ const getCardValue = require("./3-get-card-value");
test("should return 11 for Ace of Spades", () => {
const aceofSpades = getCardValue("A♠");
expect(aceofSpades).toEqual(11);
});
})

// Case 2: Handle Number Cards (2-10):
// Case 3: Handle Face Cards (J, Q, K):
// Case 4: Handle Ace (A):
// Case 5: Handle Invalid Cards:
// Case 2: Handle Number Cards (2-10)
test("should return numeric value for number cards", () => {
expect(getCardValue("5♥")).toBe(5);
expect(getCardValue("9♦")).toBe(9);
expect(getCardValue("2♣")).toBe(2);
expect(getCardValue("10♦")).toBe(10);
});

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

// Case 4: Handle Invalid Cards
test("should throw an error for invalid card ranks", () => {
expect(() => getCardValue("Z♣")).toThrow("Invalid card rank");
expect(() => getCardValue("1♦")).toThrow("Invalid card rank");
expect(() => getCardValue("X♥")).toThrow("Invalid card rank");
expect(() => getCardValue("")).toThrow("Invalid card rank");
});
15 changes: 13 additions & 2 deletions Sprint-3/3-mandatory-practice/implement/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
}

let count = 0;
// Loop through the string and count occurrences of `findCharacter`
for (let i = 0; i < stringOfCharacters.length; i++) {
if (stringOfCharacters[i] === findCharacter) {
count++;
}
}
return count;
}




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 return 0 if the character does not exist", () => {
const str = "hello";
const char = "z";
const count = countChar(str, char);
expect(count).toEqual(0); // 'z' does not exist in "hello"
});
24 changes: 22 additions & 2 deletions Sprint-3/3-mandatory-practice/implement/get-ordinal-number.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
function getOrdinalNumber(num) {
return "1st";
function getOrdinalNumber(num) { // Convert the number to a string to easily access its last digits
const lastDigit = num % 10;
const lastTwoDigits = num % 100;

// Handle special cases for numbers 11-13
if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
return `${num}th`;
}

// Handle normal cases for 1st, 2nd, 3rd, etc.
switch (lastDigit) {
case 1:
return `${num}st`;
case 2:
return `${num}nd`;
case 3:
return `${num}rd`;
default:
return `${num}th`;
}


}

module.exports = getOrdinalNumber;
Loading