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,19 @@

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

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

// invalid angle
const invalid = getAngleType(0);
assertEquals(invalid, "Invalid angle");

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

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

// other invalid angles
assertEquals(getAngleType(360), "Invalid angle");
assertEquals(getAngleType(-180), "Invalid angle");
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
// execute the code to ensure all tests pass.

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
// should change negative numbers to absolute numbers
return Math.abs(numerator) < Math.abs(denominator);
}

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

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


// Tests
assertEquals(isProperFraction(2, 3), true);
assertEquals(isProperFraction(3, 1), false);
assertEquals(isProperFraction(-1, -6), true);
assertEquals(isProperFraction(-1, 0), false);
assertEquals(isProperFraction(1, 1), false);
assertEquals(isProperFraction(-6, 6), false);

assertEquals(isProperFraction(-1, -2), true);
assertEquals(isProperFraction(1, -2), true);
assertEquals(isProperFraction(-1, 2), true);
assertEquals(isProperFraction(1, 0), false);
assertEquals(isProperFraction(-1, 0), false);
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,22 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
const ranks = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];
const suits = ["♠", "♥", "♦", "♣"];
const rankPart = card.slice(0, card.length - 1);
const suitPart = card.slice(-1);

if (ranks.includes(rankPart) && suits.includes(suitPart)) {
if (rankPart == "A") {
return 11;
} else if (rankPart == "J" || rankPart == "Q" || rankPart == "K") {
return 10;
} else {
return Number(rankPart);
}
} else {
throw new Error("Invalid card. A valid card should contain a rank followed by the suit");
}
}

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

assertEquals(getCardValue("10♣"), 10);
assertEquals(getCardValue("J♥"), 10);
assertEquals(getCardValue("K♥"), 10);
assertEquals(getCardValue("A♦"), 11);
assertEquals(getCardValue("2♦"), 2);

// Handling invalid cards
try {
getCardValue("invalid");
Expand All @@ -50,3 +71,19 @@ try {
} catch (e) {}

// What other invalid card cases can you think of?
// Handling invalid cards
function assertThrows(testFunction) {
let threw = false;

try {
testFunction();
} catch (error) {
threw = true;
}

console.assert(threw, "Expected function to throw an error.")
}

assertThrows(() => getCardValue("1♣"));
assertThrows(() => getCardValue("12"));
assertThrows(() => getCardValue("♦3"));
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,34 @@ 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(135)).toEqual("Obtuse angle");
expect(getAngleType(179)).toEqual("Obtuse angle");
expect(getAngleType(90.1)).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(270)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
})

// Case 6: Invalid angles
test(`should return "Invalid angle" for angles outside the valid range`, () => {
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(-90)).toEqual("Invalid angle");
expect(getAngleType(-0.9)).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 true when abs(numerator) < abs(denominator)`, () => {
expect(isProperFraction(0, 1)).toEqual(true);
expect(isProperFraction(-3, -6)).toEqual(true);
expect(isProperFraction(1, 2)).toEqual(true);
})

test(`should return false when abs(numerator) > abs(denominator)`, () => {
expect(isProperFraction(2, 1)).toEqual(false);
expect(isProperFraction(-2, -1)).toEqual(false);
})

Copy link
Contributor

Choose a reason for hiding this comment

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

You have more comprehensive test data in Sprint-3/1-implement-and-rewrite-tests/implement/2-is-proper-fraction.js.

test(`should return false when (numerator == denominator)`, () => {
expect(isProperFraction(0, 0)).toEqual(false);
})
Comment on lines +23 to +25
Copy link
Contributor

Choose a reason for hiding this comment

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

This test would have missed detecting a bug in this implementation:

function isProperFraction(numarator, denominator) {
  if (denominator == 0) return false;
  return Math.abs(numarator) <= Math.abs(denominator);
}

Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,26 @@ test(`Should return 11 when given an ace card`, () => {
// please refer to the Jest documentation:
// https://jestjs.io/docs/expect#tothrowerror

// Number Cards (2-10)
test(`should return the numeric value of number cards`, () => {
expect(getCardValue("2♠")).toEqual(2);
expect(getCardValue("10♥")).toEqual(10);
expect(getCardValue("9♦")).toEqual(9);
expect(getCardValue("4♣")).toEqual(4);
expect(getCardValue("5♦")).toEqual(5);
})

// Face Cards (J, Q, K)
test(`should return 10 for face cards ("J", "Q", "K")`, () => {
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("Q♣")).toEqual(10);
expect(getCardValue("K♥")).toEqual(10);
})

// Invalid Cards
test(`should throw for invalid cards`, () => {
expect(() => getCardValue("1♠")).toThrow();
expect(() => getCardValue("11")).toThrow();
expect(() => getCardValue("♣10")).toThrow();
expect(() => getCardValue("♣")).toThrow();
})
Loading
Loading