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 @@ -16,6 +16,20 @@

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 @@ -35,3 +49,29 @@ function assertEquals(actualOutput, targetOutput) {
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");

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

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

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

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

// Boundary tests
assertEquals(getAngleType(1), "Acute angle");
assertEquals(getAngleType(359), "Reflex angle");

// Invalid angle tests
assertEquals(getAngleType(0), "Invalid angle");
assertEquals(getAngleType(360), "Invalid angle");
assertEquals(getAngleType(-10), "Invalid angle");
assertEquals(getAngleType(500), "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@

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

if (denominator === 0) {
return false;
}

if (Math.abs(numerator) < Math.abs(denominator)) {
return true;
}

return false;
}

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

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

// numerator smaller than denominator
assertEquals(isProperFraction(3, 4), true);

// numerator equal to denominator (not proper)
assertEquals(isProperFraction(5, 5), false);

// numerator larger than denominator
assertEquals(isProperFraction(7, 3), false);

// numerator is zero
assertEquals(isProperFraction(0, 5), true);

// denominator is zero (invalid)
assertEquals(isProperFraction(1, 0), false);

// negative numbers
assertEquals(isProperFraction(-1, 2), true);
assertEquals(isProperFraction(1, -2), true);
assertEquals(isProperFraction(-3, -2), false);
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,30 @@

function getCardValue(card) {
// TODO: Implement this function

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

if (typeof card !== "string" || card.length < 2) {
throw new Error("Invalid card");
}

const suit = card.slice(-1);
const rank = card.slice(0, -1);

if (!suits.includes(suit)) {
throw new Error("Invalid card");
}

if (rank === "A") return 11;
if (rank === "J" || rank === "Q" || rank === "K") return 10;

const numberValue = Number(rank);

if (numberValue >= 2 && numberValue <= 10) {
return numberValue;
}
Comment on lines +43 to +47
Copy link
Contributor

Choose a reason for hiding this comment

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

In JavaScript, strings that represent valid numeric literals in the language can be safely converted to equivalent numbers. For examples, "0x02", "2.1", or "0002".

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

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


throw new Error("Invalid card");
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -41,6 +65,18 @@ function assertEquals(actualOutput, targetOutput) {
// Examples:
assertEquals(getCardValue("9♠"), 9);

// Ace
assertEquals(getCardValue("A♠"), 11);

// Face cards
assertEquals(getCardValue("J♥"), 10);
assertEquals(getCardValue("Q♦"), 10);
assertEquals(getCardValue("K♣"), 10);

// Number cards
assertEquals(getCardValue("2♠"), 2);
assertEquals(getCardValue("10♥"), 10);

// Handling invalid cards
try {
getCardValue("invalid");
Expand All @@ -49,4 +85,15 @@ try {
console.error("Error was not thrown for invalid card");
} catch (e) {}

// What other invalid card cases can you think of?
// More invalid cases
try {
getCardValue("11♠");
console.error("Error was not thrown for invalid card");
} catch (e) {}

try {
getCardValue("A");
console.error("Error was not thrown for invalid card");
} catch (e) {}

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