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
21 changes: 16 additions & 5 deletions Sprint-3/1-key-implement/1-get-angle-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@
// The assertion error will tell you what the expected output is
// Write the code to pass the test
// 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
}

*/
function getAngleType(angle) {
if (angle === 90) return "Right angle";
if (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
// if the actual output matches the target output, the test will pass
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
`Expected ${actualOutput} to equal ${targetOutput}`
);
}

Expand All @@ -44,13 +51,17 @@ assertEquals(acute, "Acute angle");
// 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
// ====> 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");
16 changes: 14 additions & 2 deletions Sprint-3/1-key-implement/2-is-proper-fraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
// complete the rest of the tests and cases
// write one test at a time, and make it pass, build your solution up methodically


function isProperFraction(numerator, denominator) {
if (numerator < denominator) return true;
if (numerator < denominator && denominator !== 0) return true;
return false;
}

// here's our helper again
Expand Down Expand Up @@ -41,13 +43,23 @@ assertEquals(improperFraction, false);
// 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?

// Zero Numerator check:

const zeroNumerator = isProperFraction(0, 5);
assertEquals(zeroNumerator, true);

// Zero Denominator check:
const zeroDenominator = isProperFraction(5, 0);
assertEquals(zeroDenominator, false);
36 changes: 31 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 @@ -8,7 +8,21 @@
// 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;
const rank = card.slice(0, -1); // Extract the rank (e.g., "A", "2", "K")
if (rank === "A") return 11;
if (rank === "2") return 2;
if (rank === "3") return 3;
if (rank === "4") return 4;
if (rank === "5") return 5;
if (rank === "6") return 6;
if (rank === "7") return 7;
if (rank === "8") return 8;
if (rank === "9") return 9;
if (rank === "10") return 10;
if (rank === "J") return 10;
if (rank === "Q") return 10;
if (rank === "K") return 10;
return 0; // Default case (shouldn't happen with valid cards)

Choose a reason for hiding this comment

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

You can put J,Q,K in same if check

}

// You need to write assertions for your function to check it works in different cases
Expand All @@ -20,6 +34,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 @@ -34,18 +49,29 @@ assertEquals(aceofSpades, 11);
// 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 tenofDiamonds = getCardValue("10♦");
assertEquals(tenofDiamonds, 10);
const jackofClubs = getCardValue("J♣");
assertEquals(jackofClubs, 10);
const queenofSpades = getCardValue("Q♠");
assertEquals(queenofSpades, 10);
const kingofHearts = getCardValue("K♥");
assertEquals(kingofHearts, 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 aceofClubs = getCardValue("A♣");
assertEquals(aceofClubs, 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,
// When the function is called with such
// a card,
// Then it should throw an error indicating "Invalid card rank."
const invalidCard = getCardValue("X♥");
assertEquals(invalidCard, 0);
20 changes: 7 additions & 13 deletions Sprint-3/2-mandatory-rewrite/1-get-angle-type.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
function getAngleType(angle) {
if (angle === 90) return "Right angle";
// replace with your completed function from key-implement

if (angle < 90 && angle > 0) 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";
if (angle === 0) return "Zero angle";
if (angle === 360) return "Full angle";
return "Invalid 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 have just written the CommonJS module.exports syntax for you
module.exports = getAngleType;
36 changes: 21 additions & 15 deletions Sprint-3/2-mandatory-rewrite/1-get-angle-type.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@ 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°)", () => {
expect(getAngleType(30)).toEqual("Acute angle");
expect(getAngleType(60)).toEqual("Acute angle");
expect(getAngleType(1)).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 (greater than 90° and less than 180°)", () => {
expect(getAngleType(120)).toEqual("Obtuse angle");
expect(getAngleType(150)).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°)", () => {
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 (greater than 180° and less than 360°)", () => {
expect(getAngleType(210)).toEqual("Reflex angle");
expect(getAngleType(270)).toEqual("Reflex angle");
expect(getAngleType(300)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});
10 changes: 8 additions & 2 deletions Sprint-3/2-mandatory-rewrite/2-is-proper-fraction.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
function isProperFraction(numerator, denominator) {
if (numerator < denominator) return true;
// add your completed function from key-implement here
if (denominator === 0) {
return false;
}
if (Math.abs(numerator) < Math.abs(denominator)) {
return true;
} else {
return false;
}
}

module.exports = isProperFraction;
14 changes: 14 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,21 @@ test("should return true for a proper fraction", () => {
});

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

// Case 3: Identify Negative Fractions:
test("should handle negative numerators and denominators", () => {
expect(isProperFraction(-1, 2)).toEqual(true);
expect(isProperFraction(1, -2)).toEqual(true);
expect(isProperFraction(-3, -2)).toEqual(false);
expect(isProperFraction(-2, -3)).toEqual(true);
});

// Case 4: Identify Equal Numerator and Denominator:
test("should return false for equal numerator and denominator", () => {
expect(isProperFraction(3, 3)).toEqual(false);
expect(isProperFraction(1, 1)).toEqual(false);
});
25 changes: 21 additions & 4 deletions Sprint-3/2-mandatory-rewrite/3-get-card-value.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
function getCardValue(card) {
// replace with your code from key-implement
return 11;
}
module.exports = getCardValue;
if (typeof card !== 'string' || card.length < 1) {
return 0; // Or throw an error, depending on desired behavior for invalid cards.
}

const value = card[0];

if (value === 'A') {
return 11;
} else if (['J', 'Q', 'K'].includes(value)) {
return 10;
} else if (['2', '3', '4', '5', '6', '7', '8', '9', '10'].includes(value)) {
if(value === '1' && card[1] === '0'){
return 10;
}
return parseInt(value);
} else {
return 0; // Or throw an error, depending on desired behavior for invalid cards.
}
}

module.exports = getCardValue;
22 changes: 22 additions & 0 deletions Sprint-3/2-mandatory-rewrite/3-get-card-value.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,28 @@ test("should return 11 for Ace of Spades", () => {
});

// Case 2: Handle Number Cards (2-10):
test("should return 2-9 for number cards", () => {
expect(getCardValue("2♥")).toEqual(2);
expect(getCardValue("5♦")).toEqual(5);
expect(getCardValue("9♣")).toEqual(9);
});
// Case 3: Handle Face Cards (J, Q, K):
test("should return 10 for face cards", () => {
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 Ace", () => {
expect(getCardValue("A♥")).toEqual(11);
expect(getCardValue("A♦")).toEqual(11);
expect(getCardValue("A♣")).toEqual(11);
});
// Case 5: Handle Invalid Cards:
test("should return 0 for invalid cards", () => {
expect(getCardValue("X")).toEqual(0);
expect(getCardValue("")).toEqual(0);
expect(getCardValue(123)).toEqual(0);
expect(getCardValue(null)).toEqual(0);
expect(getCardValue(undefined)).toEqual(0);
});
16 changes: 11 additions & 5 deletions Sprint-3/3-mandatory-practice/implement/count.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function countChar(stringOfCharacters, findCharacter) {
return 5
}

module.exports = countChar;
const countChar = (str, char) => {
let count = 0;
for (let i = 0; i < str.length; i++) {
if (str[i] === char) {
count++;
}
}
return count;
};

module.exports = countChar;
34 changes: 34 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,37 @@ 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.
// 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 does not exist", () => {
const str = "hello";
const char = "z";
const count = countChar(str, char);
expect(count).toEqual(0);
});

test("should handle empty string", () => {
const str = "";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(0);
});

test("should handle empty character", () => {
const str = "abc";
const char = "";
const count = countChar(str, char);
expect(count).toEqual(0);
});

test("should handle string with different case", () => {
const str = "aBcDa";
const char = "a";
const count = countChar(str, char);
expect(count).toEqual(2);
});

Loading