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
@@ -1,3 +1,4 @@

// Implement a function getAngleType
//
// When given an angle in degrees, it should return a string indicating the type of angle:
Expand All @@ -13,9 +14,19 @@
// Acceptance criteria:
// 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
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 @@ -34,4 +45,22 @@ function assertEquals(actualOutput, targetOutput) {
// TODO: Write tests to cover all cases, including boundary and invalid cases.
// Example: Identify Right Angles
const right = getAngleType(90);
assertEquals(right, "Right angle");
assertEquals(right, "right angle");

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

const obtuse = getAngleType(120);
assertEquals(obtuse,'obtuse angle')

const straight = getAngleType(180);
assertEquals(straight, 'straight angle');

const reflex = getAngleType(270);
assertEquals(reflex, 'reflex angle');

const invalid1 = getAngleType(-10);
assertEquals(invalid1, 'Invalid angle');

const invalid2 = getAngleType(400);
assertEquals(invalid2, 'Invalid angle');
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@

function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (denominator === 0){
return false;

}
if (numerator < denominator && numerator > 0){
return true;
}
return false;
}

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

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

// example: 4/3 is not a proper fraction
assertEquals(isProperFraction(4, 3), false);

// example: 6/5 is not a proper fraction
assertEquals(isProperFraction(6, 5), false);

// example: 0/5 is a proper fraction
assertEquals(isProperFraction(0, 5), true);

// example: 5/0 is not a proper fraction
assertEquals(isProperFraction(5, 0), false);
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,23 @@
// execute the code to ensure all tests pass.

function getCardValue(card) {
// TODO: Implement this function
const rank = card.slice(0, -1); // everything except the last character (suit)
const validSuits = ["♠", "♥", "♦", "♣"];
const suit = card.slice(-1);

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

if (rank === "A") {
return 11;
} else if (rank === "J" || rank === "Q" || rank === "K") {
return 10;
} else if (["2","3","4","5","6","7","8","9","10"].includes(rank)) {
return Number(rank);
} else {
throw new Error("Invalid card");
}
}

// The line below allows us to load the getCardValue function into tests in other files.
Expand All @@ -46,9 +62,37 @@ 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 😢");
console.error("Error was not thrown for invalid card ");
} catch (e) {
console.log("Error thrown for invalid card 🎉");
console.log("Error thrown for invalid card ");
}

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

// What other valid card cases can you think of?
function assertEqual(func, message) {
try {
func();
console.log("Valid card test passed: " + message);
} catch (e) {
console.error("Valid card test failed: " + message + " — " + e.message);
}
}
// What other invalid card cases can you think of?
function assertThrows(func, errorMessage) {
try {
func();
console.error("Error was not thrown ");
} catch (e) {
if (e.message === errorMessage) {
console.log("Error thrown as expected ");
} else {
console.error(`Unexpected error message: ${e.message}`);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,36 @@ const getAngleType = require("../implement/1-get-angle-type");
// including boundary and invalid cases.

// Case 1: Acute angles
test(`should return "Acute angle" when (0 < angle < 90)`, () => {
test(`should return "Acute angle" when (0 < angle && angle < 90)`, () => {
// Test various acute angles, including boundary cases
expect(getAngleType(1)).toEqual("Acute angle");
expect(getAngleType(45)).toEqual("Acute angle");
expect(getAngleType(89)).toEqual("Acute angle");
});

// Case 2: Right angle
test (`should return "right angle" when (angle === 90)`,() => {
// testing various angles ,including boundary cases
expect( getAngleType(90)).toEqual("right angle");
});
// Case 3: Obtuse angles
test (`should return "obtuse angle" when (90 < angle && angle < 180)`, () => {
expect(getAngleType(91)).toEqual("obtuse angle");
expect(getAngleType(125)).toEqual("obtuse angle");
expect(getAngleType(172)).toEqual("obtuse angle");
});
// Case 4: Straight angle
// Case 5: Reflex angles
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 && 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" when (angle < 0 || angle > 360)`, () => {
expect(getAngleType(-1)).toEqual("Invalid angle");
expect(getAngleType(361)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,17 @@ const isProperFraction = require("../implement/2-is-proper-fraction");
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
});

// special case : numerator is one
test(` should return true when the numerator is one and denominator is greater than one`, () => {
expect(isProperFraction(1, 2)).toEqual(true);
});
// special case : denominator is negative to the numerator
test(`should return false when the denominator is negative and the numerator is positive`, () => {
expect(isProperFraction(1, -2)).toEqual(false);
});

// special case : denominator is negative to the numerator
test(`should return false when the numerator is negative and the denominator is positive`, () => {
expect(isProperFraction(-1, 2)).toEqual(false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,24 @@ test(`Should return 11 when given an ace card`, () => {
expect(getCardValue("A♠")).toEqual(11);
});

// Suggestion: Group the remaining test data into these categories:
// Number Cards (2-10)
// Face Cards (J, Q, K)
// Invalid Cards
// case 2 : face cards (J, Q, K)
test(`Should return 10 when given a face card`, () => {
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("Q♥")).toEqual(10);
expect(getCardValue("K♦")).toEqual(10);
});

// Case 3: Number Cards (2-10)
test(`Should return the correct value when given a number card`, () => {
expect(getCardValue("2♠")).toEqual(2);
expect(getCardValue("5♥")).toEqual(5);
expect(getCardValue("10♦")).toEqual(10);
});

// Case 4: Invalid Cards
test(`Should throw an error when given an invalid card`, () => {
expect(() => getCardValue("invalid")).toThrow("Invalid card");
});

// To learn how to test whether a function throws an error as expected in Jest,
// please refer to the Jest documentation:
Expand Down
Loading