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

WM4 | Fatima Safana | Module-Structuring-and-Testing-Data | WEEK6 #235

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 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: 19 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,22 @@
// Identify Reflex Angles:
// When the angle is greater than 180 degrees and less than 360 degrees,
// Then the function should return "Reflex angle"

module.exports = getAngleType

function getAngleType(angle) {
if (angle === 90) {
return "Right angle"
} else if ( angle < 90) {
Copy link

@cjyuan cjyuan Dec 22, 2024

Choose a reason for hiding this comment

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

The spec does not give an exact specification of acute angle.
By definition, an acute angle cannot be less than or equal to 0.

return "Acute angle"
} else if (angle > 90 && angle < 180){
return "Obtuse angle"
} else if (angle === 180 ){
return "Straight angle"
} else if (angle > 180 && angle < 360) {
return "Reflex angle"
} else {
return "not valid input"
}
}
console.log(getAngleType(400))
20 changes: 20 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,23 @@
// 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."

module.exports = getCardValue

function getCardValue(card) {
const singleDigitRank = Number(card.substring(0,1));
// const doubleDigitRank = Number(card.substring(0,2));

if (card.startsWith("10")){
return 10;
} else if (singleDigitRank >= 2 && singleDigitRank <=9){
return singleDigitRank;
} else if (card.startsWith("J") || card.startsWith("Q") || card.startsWith("K")){
return 10;
} else if (card.startsWith("A")){
return 11
} else return "Invalid Cards";

}
Copy link

@cjyuan cjyuan Dec 22, 2024

Choose a reason for hiding this comment

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

What do you expect from the following function calls?
Does your function return the value you expected?

getCardValue("99Q♠");
getCardValue("100♠");
getCardValue("2K♠");
getCardValue("KK♠");
getCardValue("AA♠")

console.log(getCardValue("J♠"));
console.log(getCardValue("Z♠"));
34 changes: 34 additions & 0 deletions Sprint-3/implement/implement-sprint3.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const getAngleType = require('./get-angle-type.js');
const isProperFraction = require('./is-proper-fraction.js');
const isValidTriangle = require('./is-valid-triangle.js');
const getCardValue = require('./get-card-value.js');



test('Given an angle in degrees, return the type of angle', () => {
Comment on lines +1 to +8
Copy link

Choose a reason for hiding this comment

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

It is better to use separate .test.js file for each unit test.

expect(getAngleType(90)).toBe("Right angle");
expect(getAngleType(45)).toBe("Acute angle");
expect(getAngleType(160)).toBe("Obtuse angle");
expect(getAngleType(180)).toBe("Straight angle");
expect(getAngleType(300)).toBe("Reflex angle");
});
Comment on lines +8 to +14
Copy link

Choose a reason for hiding this comment

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

Could also test invalid cases.


test('Given numerator and denominator, return the type of fraction', () => {
expect(isProperFraction(2, 3)).toBe(true);
expect(isProperFraction(5, 3)).toBe(false);
expect(isProperFraction(2, 0)).toBe("error");
});
Copy link

Choose a reason for hiding this comment

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

Why not test also cases where numerator and/or denominator are negative values?



test('Given length of sides validate the its a triangle', () => {
expect(isValidTriangle(3, 3, 3)).toBe(true);
expect(isValidTriangle(0, 3, 3)).toBe(false);
expect(isValidTriangle(3, 1, 1)).toBe(false);
});

test('Given the card input getCardValue returns correct number for valid ranks ', () => {
expect(getCardValue("5♥")).toBe(5);
expect(getCardValue("K♥")).toBe(10);
expect(getCardValue("A♥")).toBe(11);
expect(getCardValue("z♥")).toBe("Invalid Cards");
});
13 changes: 13 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,16 @@
// 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.

module.exports = isProperFraction

function isProperFraction(numerator, denominator){
if (denominator === numerator){
return false
} else if (denominator === 0 ) {
return "error"
} else if (denominator < numerator) {
return false
} else (denominator > numerator);
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.

15 changes: 15 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,18 @@
// 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.

module.exports = isValidTriangle

function isValidTriangle(a, b, c){
//checks for valid input
if (a <= 0 || b <= 0 || c <= 0 ){
return false
//checks for invalid triangle
} else if (((a + b) <= c) || ((c + b) <= a) ||((a + c) <= b)){
return false
} else return true;
}

console.log(isValidTriangle(3, 3, 3))
//else if (((a + b) > c) || ((c + b) > a) ||((a + c) > b)){
Loading