Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 @@ -14,8 +14,22 @@
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.

function getAngleType(angle) {
// TODO: Implement this function
function getAngleType(angle)
{
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 @@ -32,6 +46,35 @@ function assertEquals(actualOutput, targetOutput) {
}

// TODO: Write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles

// Acute angle
const acute = getAngleType(45);
assertEquals(acute, "Acute angle");

// Right angle
const right = getAngleType(90);
assertEquals(right, "Right angle");

// Obtuse angle
const obtuse = getAngleType(120);
assertEquals(obtuse, "Obtuse angle");

// Straight angle
const straight = getAngleType(180);
assertEquals(straight, "Straight angle");

// Reflex angle
const reflex = getAngleType(270);
assertEquals(reflex, "Reflex angle");

// Invalid angle (too small)
const invalidLow = getAngleType(0);
assertEquals(invalidLow, "Invalid angle");

// Invalid angle (negative)
const invalidNegative = getAngleType(-10);
assertEquals(invalidNegative, "Invalid angle");

// Invalid angle (too large)
const invalidHigh = getAngleType(360);
assertEquals(invalidHigh, "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function

function isProperFraction(numerator, denominator) {
if (numerator > 0 && denominator > 0 && numerator < denominator) {
return true;
} else {
return false;
}
}

// The line below allows us to load the isProperFraction function into tests in other files.
Expand All @@ -31,3 +36,15 @@ function assertEquals(actualOutput, targetOutput) {

// Example: 1/2 is a proper fraction
assertEquals(isProperFraction(1, 2), true);

// Numerator > denominator
assertEquals(isProperFraction(4, 1), false);

// Numerator === denominator
assertEquals(isProperFraction(2, 2), false);

// Numerator is 0
assertEquals(isProperFraction(0, 4), false);

// Denominator is 0
assertEquals(isProperFraction(6, 0), false);
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,32 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
const rank = card.substring(0, card.length - 1);
const suit = card.slice(-1);

const validRanks = [
"A", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "J", "Q", "K"
];

const validSuits = ["♠", "♥", "♦", "♣"];

if (!validRanks.includes(rank) || !validSuits.includes(suit)) {
throw new Error("Invalid card");
}

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

if (rank === "J" || rank === "Q" || rank === "K") {
return 10;
}

return Number(rank);
}
// TODO: Implement this function


// 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.
Expand All @@ -39,16 +63,49 @@ function assertEquals(actualOutput, 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("Q♠"), 10);
assertEquals(getCardValue("6♠"), 6);

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

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

// What other invalid card cases can you think of?

try {
getCardValue("11♠");

console.error("Error was not thrown for invalid card 😢");
} catch (e) {
console.log("Error thrown for invalid card 🎉");
}

try {
getCardValue("X♠");

console.error("Error was not thrown for invalid card 😢");
} catch (e) {
console.log("Error thrown for invalid card 🎉");
}

try {
getCardValue("AA");

console.error("Error was not thrown for invalid card 😢");
} catch (e) {
console.log("Error thrown for invalid card 🎉");
}

try {
getCardValue("9");

console.error("Error was not thrown for invalid card 😢");
} catch (e) {
console.log("Error thrown for invalid card 🎉");
}
Loading