-
-
Notifications
You must be signed in to change notification settings - Fork 218
LONDON | ITP-MAY-25 | TEWODROS BEKERE | Coursework/sprint-3 #602
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
Changes from 15 commits
9167e67
f9da79b
da2169b
6894bd8
cb1730c
55d8c9f
3a6390e
796a8cf
b014b98
7e10cb8
0771e60
c8ed1bb
e9ab61e
04360b4
ce4fb74
63dda1e
ab8f199
d96efd6
1fc4d18
3ea90b1
76a9808
5ed0d66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,15 @@ | |
// 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) return true; | ||
return false; | ||
// Note: This function currently does not handle cases where the denominator is zero. | ||
// If you want to handle that case, you can add a check for it. | ||
// For example, you could add a case that returns false if the denominator is zero. | ||
// If you want to handle negative fractions, you can also add a case for that. | ||
// If you want to handle fractions where the numerator is equal to the denominator, | ||
// you can also add a case for that. | ||
// If you want to handle fractions that are not numbers, you can also add a case for that. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see that you have added comments for handling edge cases for this function, seems like your requirements are well defined. Perhaps this would be a good stretch for you to introduce logic for handling the denominator being zero. How would you handle the situation where the numerator is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added the zero denominator to throw an error, and for this logic (-4, 3) it's handled by ' if (numerator < denominator) return true;' There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for adding that check to make sure the denominator is non-zero. |
||
} | ||
|
||
// here's our helper again | ||
|
@@ -41,13 +49,17 @@ 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? | ||
|
||
console.log(isProperFraction(3, 3)); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
// 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; | ||
if (rank === "A") return 11; | ||
} | ||
|
||
// You need to write assertions for your function to check it works in different cases | ||
|
@@ -25,27 +25,69 @@ 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♠"); | ||
assertEquals(aceofSpades, 11); | ||
const ace_of_Spades = getCardValue("A♠"); | ||
|
||
assertEquals(ace_of_Spades, 11); | ||
|
||
// Handle Number Cards (2-10): | ||
// Given a card with a rank between "2" and "9", | ||
// 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 | ||
|
||
// 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. | ||
|
||
// 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. | ||
// Handle Jack (J): | ||
// Given a card with a rank of "J", | ||
// When the function is called with a Jack, | ||
// Then it should return 10 points, as Jacks are worth 10 points in blackjack. | ||
const jack_of_Diamonds = getCardValue("J♦"); | ||
assertEquals(jack_of_Diamonds, 10); | ||
|
||
// Handle Queen (Q): | ||
// Given a card with a rank of "Q", | ||
// When the function is called with a Queen, | ||
// Then it should return 10 points, as Queens are worth 10 points in blackjack. | ||
const queen_of_Hearts = getCardValue("Q♥"); | ||
assertEquals(queen_of_Hearts, 10); | ||
|
||
// Handle King (K): | ||
// Given a card with a rank of "K", | ||
// When the function is called with a King, | ||
// Then it should return 10 points, as Kings are worth 10 points in blackjack. | ||
const king_of_Clubs = getCardValue("K♣"); | ||
assertEquals(king_of_Clubs, 10); | ||
|
||
// Handle Number Cards (2-10): | ||
// Given a card with a rank between "2" and "10", | ||
// 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, "10" should return 10). | ||
const two_of_Spades = getCardValue("2♠"); | ||
assertEquals(two_of_Spades, 2); | ||
const three_of_Hearts = getCardValue("3♥"); | ||
assertEquals(three_of_Hearts, 3); | ||
const four_of_Diamonds = getCardValue("4♦"); | ||
assertEquals(four_of_Diamonds, 4); | ||
const five_of_Hearts = getCardValue("5♥"); | ||
assertEquals(five_of_Hearts, 5); | ||
const six_of_Clubs = getCardValue("6♣"); | ||
assertEquals(six_of_Clubs, 6); | ||
const seven_of_Spades = getCardValue("7♠"); | ||
assertEquals(seven_of_Spades, 7); | ||
const eight_of_Hearts = getCardValue("8♥"); | ||
assertEquals(eight_of_Hearts, 8); | ||
const nine_of_Diamonds = getCardValue("9♦"); | ||
assertEquals(nine_of_Diamonds, 9); | ||
|
||
// 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." | ||
function getCardValue(card) { | ||
|
||
const rank = card.slice(0, -1); // Extract the rank part of the card string | ||
if (rank === "A") return 11; // Ace is worth 11 points | ||
if (["J", "Q", "K"].includes(rank)) return 10; // Face cards are worth 10 points | ||
const numericRank = parseInt(rank, 10); // Convert rank to a number | ||
if (numericRank >= 2 && numericRank <= 10) return numericRank; // Number cards return their value | ||
throw new Error("Invalid card rank"); // Throw an error for invalid ranks | ||
} | ||
|
||
console.log(getCardValue("A♠")); // 11 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,24 @@ | ||
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"; | ||
} else if (angle === 90) { | ||
return "Right angle"; | ||
} else if (angle > 90 && angle < 180) { | ||
return "Obtuse angle"; | ||
} else if (angle === 180) { | ||
return "Straight angle"; | ||
}else if (angle > 180 && angle < 360) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function looks good and covers the requirements. Nice job. The two checks for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This satisfies the criteria. |
||
return "Reflex angle"; | ||
} | ||
else { | ||
return "Invalid angle"; | ||
} | ||
|
||
} | ||
|
||
|
||
console.log(getAngleType(-90)); // Right angle | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the comment match the output for this statement? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,47 @@ | ||
const getAngleType = require("./1-get-angle-type"); | ||
const getAngleType = require("./1-get-angle-type"); | ||
|
||
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 angle (<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 (>90° and <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 (>180° and <360°)", () => { | ||
expect(getAngleType(270)).toEqual("Reflex angle"); | ||
}); | ||
// Case 6: Handle Invalid Angles: | ||
// When the angle is less than 0 degrees or greater than or equal to 360 degrees, | ||
// Then the function should return "Invalid angle" | ||
test("should handle invalid angle (<0° or >=360°)", () => { | ||
expect(getAngleType(-10)).toEqual("Invalid angle"); | ||
expect(getAngleType(360)).toEqual("Invalid angle"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is good that you used two expect statements here, for future tests this is a good practice. |
||
}); | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,13 @@ | ||
function isProperFraction(numerator, denominator) { | ||
if (numerator < denominator) return true; | ||
// add your completed function from key-implement here | ||
if (denominator === 0) return false; // Avoid division by zero | ||
if (numerator === 0) return true; // Zero is considered a proper fraction | ||
if (numerator < 0 || denominator < 0) return false; // Negative fractions are not proper | ||
return Math.abs(numerator) < Math.abs(denominator); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks good. Here would the |
||
} | ||
|
||
console.log(isProperFraction(0, 1)); | ||
console.log(isProperFraction(5,3)); | ||
|
||
module.exports = isProperFraction; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,28 @@ const isProperFraction = require("./2-is-proper-fraction"); | |
|
||
test("should return true for a proper fraction", () => { | ||
expect(isProperFraction(2, 3)).toEqual(true); | ||
expect(isProperFraction(1, 2)).toEqual(true); | ||
}); | ||
|
||
// Case 2: Identify Improper Fractions: | ||
|
||
test("should return false for an improper fraction", () => { | ||
expect(isProperFraction(5, 3)).toEqual(false); | ||
expect(isProperFraction(3, 3)).toEqual(false); | ||
}); | ||
|
||
// Case 3: Identify Negative Fractions: | ||
|
||
test("should return false for negative fractions", () => { | ||
expect(isProperFraction(-2, 3)).toEqual(false); | ||
expect(isProperFraction(2, -3)).toEqual(false); | ||
expect(isProperFraction(-2, -3)).toEqual(false); | ||
}); | ||
|
||
// Case 4: Identify Equal Numerator and Denominator: | ||
|
||
test("should return false when numerator and denominator are equal", () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test coverage looks good. |
||
expect(isProperFraction(4, 4)).toEqual(false); | ||
}); | ||
|
||
|
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; | ||
//return 11; | ||
if (card === 'A') { | ||
return 11; // Ace can be 11 or 1, but we'll | ||
} | ||
else if (card === 'K' || card === 'Q' || card === 'J') { | ||
return 10; // Face cards are worth 10 | ||
} | ||
else { | ||
return parseInt(card); // Number cards are worth their face value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a good start, how would we handle cases where there is a rank and a number? (e.g. What about situations where the rank is a number that we do not expect? e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It throws an error: invalid card rank. |
||
} | ||
} | ||
console.log(getCardValue('A')); | ||
console.log(getCardValue('K')); | ||
console.log(getCardValue('J')); | ||
console.log(getCardValue('Q')); | ||
console.log(getCardValue('B')); | ||
|
||
module.exports = getCardValue; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function looks good.
Could you use the conditional
angle <= 0 || angle >= 360
to decide whether the parameter is an"Invalid Angle"
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it works for < 0 and exact 0 and >360 and exact 360 returns invalid.
