Skip to content
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

West Midlands | Alireza Seifi Shalamzari | Module-Structuring-and-Testing-Data | Week 3 #231

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
Binary file added Sprint-3/Test Images/get-angle-type.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Sprint-3/Test Images/get-card-value.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Sprint-3/Test Images/is-proper-fraction.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Sprint-3/Test Images/is-valid-triangle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 47 additions & 0 deletions Sprint-3/implement/get-angle-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,50 @@
// Identify Reflex Angles:
// When the angle is greater than 180 degrees and less than 360 degrees,
// Then the function should return "Reflex angle"

function getAngleType(angle) {
// Check if the angle is exactly 90 degrees
if (angle === 90) {
return "Right angle";
}
// Check if the angle is less than 90 degrees
else if (angle < 90) {
return "Acute angle";
}
// Check if the angle is greater than 90 degrees and less than 180 degrees
else if (angle > 90 && angle < 180) {
return "Obtuse angle";
}
else if (angle === 180) {
return "Straight angle";
}
// Check if the angle is greater than 180 degrees and less than 360 degrees
else if (angle > 180 && angle < 360) {
return "Reflex angle";
}
//check if the angle is not an angle returns an error
else {
return "Invalid angle";
}
}
// Test the function with different angles
console.log(getAngleType(90)); // Should print "Right angle"
console.log(getAngleType(45)); // Should print "Acute angle"
console.log(getAngleType(135)); // Should print "Obtuse angle"
console.log(getAngleType(180)); // Should print "Straight angle"
console.log(getAngleType(225)); // Should print "Reflex angle"
console.log(getAngleType(360)); // Should print "Reflex angle"
console.log(getAngleType(0)); // Should print "Invalid angle"
console.log(getAngleType(-45)); // Should print "Invalid angle"
Copy link

Choose a reason for hiding this comment

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

Does these two show "Invalid angle"?

It is not easy to spot the error from just console.log() output. That's one reason why a test framework like Jest can help us identify cases when a return value does not match an expected value.

Copy link
Author

Choose a reason for hiding this comment

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

I changed the testing method to Jest

console.log(getAngleType(45.5)); // Should print "Invalid angle"
console.log(getAngleType("45")); // Should print "Invalid angle"
console.log(getAngleType(null)); // Should print "Invalid angle"
console.log(getAngleType(undefined)); // Should print "Invalid angle"
console.log(getAngleType(true)); // Should print "Invalid angle"
console.log(getAngleType(false)); // Should print "Invalid angle"
console.log(getAngleType(NaN)); // Should print "Invalid angle"
console.log(getAngleType(Infinity)); // Should print "Invalid angle"
console.log(getAngleType(-Infinity)); // Should print "Invalid angle"
console.log(getAngleType("hello")); // Should print "Invalid angle"
console.log(getAngleType([1, 2, 3])); // Should print "Invalid

46 changes: 46 additions & 0 deletions Sprint-3/implement/get-card-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,49 @@
// 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) {
// Remove the suit from the card string
const rank = card.slice(0, -1);

// Handle Number Cards (2-9)
if (rank >= "2" && rank <= "9") {
Copy link

Choose a reason for hiding this comment

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

Todo

This check is not solid enough.
You can test your function by calling getCardValue("23♠") or getCardValue("899♠") to see what they return.

Copy link
Author

Choose a reason for hiding this comment

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

I did work on this and I found a solution!
I added a set of valid ranks so only the valid ones can return an actual value

return parseInt(rank);
}

// Handle Face Cards (10, J, Q, K)
if (rank === "10" || rank === "J" || rank === "Q" || rank === "K") {
return 10;
}

// Handle Ace (A)
if (rank === "A") {
return 11;
}

// Handle Invalid Cards
throw new Error("Invalid card rank.");
}


// Assertions to test the function
try {
console.assert(getCardValue("2♠") === 2, "Test Case 1 Failed");
console.assert(getCardValue("5♦") === 5, "Test Case 2 Failed");
console.assert(getCardValue("10♥") === 10, "Test Case 3 Failed");
console.assert(getCardValue("J♣") === 10, "Test Case 4 Failed");
console.assert(getCardValue("Q♠") === 10, "Test Case 5 Failed");
console.assert(getCardValue("K♦") === 10, "Test Case 6 Failed"); //using 10 it should pass and 20 should fail
console.assert(getCardValue("A♣") === 11, "Test Case 7 Failed");

// Testing invalid card
try {
getCardValue("Z♠");
} catch (e) {
console.assert(e.message === "Invalid card rank.", "Test Case 8 Failed");
}

console.log("All test cases passed!");
} catch (error) {
console.error(error.message);
}
18 changes: 18 additions & 0 deletions Sprint-3/implement/is-proper-fraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,21 @@
// 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.
// These acceptance criteria cover a range of scenarios to ensure that the isProperFraction function handles both proper and improper fractions correctly and handles potential errors such as a zero denominator.

function isProperFraction(numerator, denominator) {
// Check if the denominator is zero and throw an error if it is
if (denominator === 0) {
throw new Error("Denominator cannot be zero");
}
// Check if the numerator is less than the denominator and return true if it is
// Otherwise, return false
return numerator < denominator;
}

//Testing the function
console.log(isProperFraction(2, 3)); //should return True
console.log(isProperFraction(5, 2)); //should return False
console.log(isProperFraction(-4, 7)); //should return True
Copy link

Choose a reason for hiding this comment

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

In mathematics, -4/7 == 4/-7, and -4/-7 == 4/7.
So, ideally isProperFraction() should recognise all of them as proper fractions.

Hint: If you compute the absolute value of both parameters inside the function first, the code can become much simpler.

Copy link
Author

Choose a reason for hiding this comment

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

I added a part which first make absolute numbers then check them

console.log(isProperFraction(3, 3)); //should return False
console.log(isProperFraction(3, 0)); //should throw an error

19 changes: 19 additions & 0 deletions Sprint-3/implement/is-valid-triangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,22 @@
// Then it should return true because the input forms a valid triangle.

// This specification outlines the behavior of the isValidTriangle function for different input scenarios, ensuring it properly checks for invalid side lengths and whether they form a valid triangle according to the Triangle Inequality Theorem.

function isValidTriangle(a, b, c) {
// Check for valid input: all sides must be greater than zero
if (a <= 0 || b <= 0 || c <= 0) {
return false;
}
// Check the Triangle Inequality: the sum of any two sides must be greater than the third
if (a + b <= c || a + c <= b || b + c <= a) {
return false;
}
return true;
}

//Testing the functionality
console.log(isValidTriangle(3, 3, 3)); // Expected output: true
console.log(isValidTriangle(3, 3, 6)); // Expected output: false
console.log(isValidTriangle(3, 0, 3)); // Expected output: false