Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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(numerator <= 0 || denominator <= 0){
return false;
}else if(numerator < denominator){
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 check if 1/-2, -1/2, -1/-2 are consider proper fractions, and then update the implementation and tests accordingly?

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.

Thank you for the suggestion — it was very useful. I checked 1/-2, -1/2, and -1/-2; all are proper fractions. I updated the isProperFraction function and tests using Math.abs() to handle these cases.

return true;
}else return false;
}

// 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(rank.match(/J|Q|K/)){
return 10;
}else if(rank.match(/^(10|[2-9])$/)){
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.

Does your function return the value you expected from each of the following function calls?

getCardValue("0x02♠");
getCardValue("QQ♠");
getCardValue("2.1♠");

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.

Thank you for pointing this out. I have tested the function with getCardValue("0x02♠"), getCardValue("QQ♠"), and getCardValue("2.1♠"); all now correctly throw an "Invalid card" error as expected. I also added Jest tests to cover these edge cases.

return Number(rank);
}else throw new Error("Invalid card");
}
// 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);
}
Loading