Skip to content
Open
Show file tree
Hide file tree
Changes from 7 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
// - "Obtuse angle" for angles greater than 90° and less than 180°
// - "Straight angle" for exactly 180°
// - "Reflex angle" for angles greater than 180° and less than 360°
// - "Invalid angle" for angles outside the valid range.
// - "(angle >= 0 || angle <= 90){
// Invalid angle" for angles outside the valid range.

// Assumption: The parameter is a valid number. (You do not need to handle non-numeric inputs.)

Expand All @@ -15,7 +16,17 @@
// execute the code to ensure all tests pass.

function getAngleType(angle) {
// TODO: Implement this function
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) {
return "Reflex angle";
} else return "Invalid angle";
}

// The line below allows us to load the getAngleType function into tests in other files.
Expand All @@ -33,5 +44,19 @@ function assertEquals(actualOutput, targetOutput) {

// TODO: Write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles
const acute = getAngleType(45);
assertEquals(acute, "Acute angle");
const right = getAngleType(90);
assertEquals(right, "Right angle");
const obtuse = getAngleType(110);
assertEquals(obtuse, "Obtuse angle");
const straight = getAngleType(180);
assertEquals(straight, "Straight angle");
const reflex = getAngleType(250);
assertEquals(reflex, "Reflex angle");
const invalid = getAngleType(380);
assertEquals(invalid, "Invalid angle");
const invalid2 = getAngleType(0);
assertEquals(invalid2, "Invalid angle");
const invalid3 = getAngleType(-10);
assertEquals(invalid3, "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if(denominator === 0){
return false;
}else if(Math.abs(numerator) < Math.abs(denominator)){
return true;
}else return false;
Comment thread
cjyuan marked this conversation as resolved.
Outdated
}

// The line below allows us to load the isProperFraction function into tests in other files.
Expand All @@ -20,8 +25,7 @@ module.exports = isProperFraction;

// Here's our helper again
function assertEquals(actualOutput, targetOutput) {
console.assert(
actualOutput === targetOutput,
console.assert(actualOutput === targetOutput,
`Expected ${actualOutput} to equal ${targetOutput}`
);
}
Expand All @@ -30,4 +34,10 @@ function assertEquals(actualOutput, targetOutput) {
// What combinations of numerators and denominators should you test?

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);
assertEquals(isProperFraction(1, 2), "true");
assertEquals(isProperFraction(3, 2), "false");
assertEquals(isProperFraction(1, 0), "false");
assertEquals(isProperFraction(8, 9), "true");
assertEquals(isProperFraction(0, 1), "false");
assertEquals(isProperFraction(-4, 1), "false");
assertEquals(isProperFraction(2, -4), "false");
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,25 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
}
if (typeof card !== "string") {
throw new Error("Invalid card");
}
let rank = card.slice(0, -1); // Get everything except the last character
let suit = card.slice(-1); // Get the last character

const validSuits = ["♠", "♥", "♦", "♣"]; // check if suit is valid
if (!validSuits.includes(suit)) {
throw new Error("Invalid card");
}

if (rank === "A"){
return 11;
}else if(/^[JQK]$/.test(rank)){
return 10;
}else if(rank.match(/^(10|[2-9])$/)){
return Number(rank);
}else throw new Error("Invalid card");
}
Comment thread
cjyuan marked this conversation as resolved.
Outdated
// The line below allows us to load the getCardValue function into tests in other files.
// This will be useful in the "rewrite tests with jest" step.
module.exports = getCardValue;
Expand All @@ -36,17 +52,48 @@ function assertEquals(actualOutput, targetOutput) {
`Expected ${actualOutput} to equal ${targetOutput}`
);
}

// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
// Examples:
assertEquals(getCardValue("9♠"), 9);
assertEquals(getCardValue("A♦"), 11);
assertEquals(getCardValue("J♣"), 10);
assertEquals(getCardValue("Q♥"), 10);
assertEquals(getCardValue("K♠"), 10);
assertEquals(getCardValue("3♦"), 3);

// Handling invalid cards
try {
getCardValue("invalid");
getCardValue("J");

// This line will not be reached if an error is thrown as expected
// The below line will not be reached if an error is thrown as expected
console.error("Error was not thrown for invalid card");
} catch (e) {}

} catch (e) {
console.log('Test passed for "J": caught error ->', e.message);
}
// What other invalid card cases can you think of?

try {
getCardValue("9X"); // invalid suit
console.error('Test failed for "9X": error was not thrown');
} catch (e) {
console.log('Test passed for "9X": caught error ->', e.message);
}

try {
getCardValue("1♠"); // invalid rank
console.error('Test failed for "1♠": error was not thrown');
} catch (e) {
console.log('Test passed for "1♠": caught error ->', e.message);
}

try {
getCardValue("0♥"); // invalid rank
console.error('Test failed for "0♥": error was not thrown');
} catch (e) {
console.log('Test passed for "0♥": caught error ->', e.message);
}

try {
getCardValue("ABC"); // completely wrong format
console.error('Test failed for "ABC": error was not thrown');
} catch (e) {
console.log('Test passed for "ABC": caught error ->', e.message);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,31 @@ test(`should return "Acute angle" when (0 < angle < 90)`, () => {
});

// Case 2: Right angle
test(`should return "Right angle" when (angle === 90)`, () => {
expect(getAngleType(90)).toEqual("Right angle");
});

// Case 3: Obtuse angles
test(`should return "Obtuse angle" when (90 < angle < 180)`, () => {
expect(getAngleType(91)).toEqual("Obtuse angle");
expect(getAngleType(150)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
});

// Case 4: Straight angle
test(`should return "Straight angle" when (angle === 180)`, () => {
expect(getAngleType(180)).toEqual("Straight angle");
});
// Case 5: Reflex angles
test(`should return "Reflex angle" when (180 < angle < 360)`, () => {
expect(getAngleType(181)).toEqual("Reflex angle");
expect(getAngleType(200)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});
// Case 6: Invalid angles
test(`should return "Invalid angle" when (angle <= 0 or angle >= 360)`, () => {
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(-40)).toEqual("Invalid angle");
expect(getAngleType(400)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,18 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});
test(`should return false when denominator is less than numerator`, () => {
expect(isProperFraction(2, 1)).toEqual(false);
});
test("should return false when numerator is zero", ()=>{
expect(isProperFraction(0, 1)).toEqual(false);
});
test("should return false when numerator is a negative number", ()=>{
expect(isProperFraction(-2, 2)).toEqual(false);
});
test("should return false when denominator is a negative number", ()=>{
expect(isProperFraction(2, -2)).toEqual(false);
});
test("should return false where both numerator and denominator are negative numbers", ()=> {
expect(isProperFraction(-3, -4)).toEqual(false);
})
Comment thread
cjyuan marked this conversation as resolved.
Outdated
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,29 @@ test(`Should return 11 when given an ace card`, () => {

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
test("Should return the value of rank variable as a number when we are given 2-10", ()=>{
expect(getCardValue("2♠")).toEqual(2);
expect(getCardValue("9♠")).toEqual(9);
expect(getCardValue("10♠")).toEqual(10);
});
// Face Cards (J, Q, K)
test(`Should return 10 when given J or Q or K`, () => {
expect(getCardValue("K♠")).toEqual(10);
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("Q♠")).toEqual(10);
})
// Invalid Cards

test("Should return Invalid card", ()=>{
expect(() => getCardValue(0)).toThrow("Invalid card");
expect(() => getCardValue("1X")).toThrow("Invalid card");
expect(() => getCardValue("0♥")).toThrow("Invalid card");
});
// Invalid card format
test("Should throw error for tricky invalid card formats", () => {
expect(() => getCardValue("0x02♠")).toThrow("Invalid card");
expect(() => getCardValue("QQ♠")).toThrow("Invalid card");
expect(() => getCardValue("2.1♠")).toThrow("Invalid card");
});
// To learn how to test whether a function throws an error as expected in Jest,
// please refer to the Jest documentation:
// https://jestjs.io/docs/expect#tothrowerror
Expand Down
5 changes: 5 additions & 0 deletions clear
Comment thread
cjyuan marked this conversation as resolved.
Outdated
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If this file is not related to the exercise, please remove it to keep the PR branch clean.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you address this comment?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

If you are talking about (isproperFracction) then it is part of the exercises. other case, I did not get what you are pointing at.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Comment can also be left on a file.
There is a file named "clear" in the top-level folder.

image

You can also view all the "changed files" on this PR branch by clicking the "Files changed" tab:
image

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
acoursework/sprint-2
acoursework/sprint-2-clean
coursework/sprint-1
* coursework/sprint-3-implement-and-rewrite
main