Skip to content
Closed
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 @@ -3,10 +3,29 @@ import test from "node:test";

import { getAngleType } from "../implement/1-get-angle-type.js";

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

test("Classifies right angles", () => {
const right = getAngleType(90);
assert.equal(right, "Right angle");
});
test("Classifies acute angles", () => {
const acute = getAngleType(60);
assert.equal(acute, "Acute angle");
});
test("Classifies obtuse angles", () => {
const obtuse = getAngleType(120);
assert.equal(obtuse, "Obtuse angle");
});
test("Classifies straight angles", () => {
const straight = getAngleType(180);
assert.equal(straight, "Straight angle");
});
test("Classifies reflex angles", () => {
const reflex = getAngleType(350);
assert.equal(reflex, "Reflex angle");
});
test("Classifies invalid angles", () => {
const invalid = getAngleType(370);
assert.equal(invalid, "Invalid angle");
});

Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,26 @@ test("Basic proper fraction", () => {
// Example: 1/2 is a proper fraction
assert.equal(isProperFraction(1, 2), true);
});

test("decimal numerator should return flase", () => {

assert.equal(isProperFraction(0.5, 2), false);
});


test("zero numerator", () => {
assert.equal(isProperFraction(0, 5), true);
});

test("numerator bigger then denumnator", () => {
assert.equal(isProperFraction(20, 15), false);
});
test("zero denumator", () => {
assert.equal(isProperFraction(100, 0), false);
});

test("equal numrator and denumerator ", () => {
assert.equal(isProperFraction(3, 3), false);
});


Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,35 @@ test("Arbitrary non-card string", () => {
});

// TODO: What other invalid card cases can you think of?
test("Ace returns 11", () => {
assert.equal(getCardValue("A♠"), 11);
});

test("Number card returns its numeric value", () => {
assert.equal(getCardValue("2♥"), 2);
assert.equal(getCardValue("7♦"), 7);
assert.equal(getCardValue("10♣"), 10);
});

test("Face cards return 10", () => {
assert.equal(getCardValue("J♣"), 10);
assert.equal(getCardValue("Q♦"), 10);
assert.equal(getCardValue("K♥"), 10);
});

test("Invalid rank throws an error", () => {
assert.throws(() => getCardValue("11♠"), Error);
assert.throws(() => getCardValue("X♥"), Error);
});

test("Invalid suit throws an error", () => {
assert.throws(() => getCardValue("A★"), Error);
});

test("Invalid format throws an error", () => {
assert.throws(() => getCardValue("A"), Error);
assert.throws(() => getCardValue("♠A"), Error);
assert.throws(() => getCardValue(" A♠"), Error);
assert.throws(() => getCardValue("A♠♠"), Error);
});

20 changes: 19 additions & 1 deletion implement-and-rewrite-tests/implement/1-get-angle-type.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Implement a function getAngleType

//
// Don't forget to write tests in implement-tests-with-node-test.
//
Expand All @@ -17,5 +18,22 @@
// execute the code to ensure all tests pass.

export function getAngleType(angle) {
// TODO: Implement this function
if (angle < 90) {
return "Acute angle";
}
if (angle == 90) {
return "Right angle";
}
if (angle < 180) {
return "Obtuse angle";
}
if (angle == 180) {
return "Straight angle";
}
if (angle < 360) {
return "Reflex angle";
}
return "Invalid angle";


}
13 changes: 12 additions & 1 deletion implement-and-rewrite-tests/implement/2-is-proper-fraction.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,16 @@
// execute the code to ensure all tests pass.

export function isProperFraction(numerator, denominator) {
// TODO: Implement this function
if (Number.isNaN(numerator) || Number.isNaN(denominator)|| denominator===0) {
return false;
}
if (!Number.isInteger(numerator) || !Number.isInteger(denominator)) {
return false;
}
if (numerator >= denominator ) {
return false;
}
return true;


}
20 changes: 18 additions & 2 deletions implement-and-rewrite-tests/implement/3-get-card-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,21 @@
// execute the code to ensure all tests pass.

export function getCardValue(card) {
// TODO: Implement this function
}
const match = /^(A|[2-9]|10|J|Q|K)([♠♥♦♣])$/.exec(card);

if (!match) {
throw new Error(`Expected a number followed by a suit, but got "${card}"`);
}

const rank = match[1];

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

if (["J", "Q", "K"].includes(rank)) {
return 10;
}

return Number(rank);
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
import { getAngleType } from "../implement/1-get-angle-type.js";

// TODO: Write tests in Jest syntax to cover all cases/outcomes,
// including boundary and invalid cases.

// Case 1: Acute angles
test(`should return "Acute angle" when (0 < 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");



test("Classifies right angles", () => {
expect(getAngleType(90)).toBe("Right angle");
});

test("Classifies acute angles", () => {
expect(getAngleType(60)).toBe("Acute angle");
});

test("Classifies obtuse angles", () => {
expect(getAngleType(120)).toBe("Obtuse angle");
});

// Case 2: Right angle
// Case 3: Obtuse angles
// Case 4: Straight angle
// Case 5: Reflex angles
// Case 6: Invalid angles
test("Classifies straight angles", () => {
expect(getAngleType(180)).toBe("Straight angle");
});

test("Classifies reflex angles", () => {
expect(getAngleType(350)).toBe("Reflex angle");
});

test("Classifies invalid angles", () => {
expect(getAngleType(370)).toBe("Invalid angle");
});


Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,50 @@ import { isProperFraction } from "../implement/2-is-proper-fraction.js";

// TODO: Write tests in Jest syntax to cover all combinations of positives, negatives, zeros, and other categories.

// Special case: numerator is zero
test(`should return false when denominator is zero`, () => {
expect(isProperFraction(1, 0)).toEqual(false);
test("should return false when denominator is zero", () => {
expect(isProperFraction(1, 0)).toBe(false);
});
test("decimal numerator should return false", () => {
expect(isProperFraction(0.5, 2)).toBe(false);
});

test("zero numerator", () => {
expect(isProperFraction(0, 5)).toBe(true);
});

test("numerator bigger than denominator", () => {
expect(isProperFraction(20, 15)).toBe(false);
});

test("zero denominator", () => {
expect(isProperFraction(100, 0)).toBe(false);
});

test("equal numerator and denominator", () => {
expect(isProperFraction(3, 3)).toBe(false);
});

test("should return false when denominator is zero", () => {
expect(isProperFraction(1, 0)).toBe(false);
});

test("decimal numerator should return false", () => {
expect(isProperFraction(0.5, 2)).toBe(false);
});

test("zero numerator", () => {
expect(isProperFraction(0, 5)).toBe(true);
});

test("numerator bigger than denominator", () => {
expect(isProperFraction(20, 15)).toBe(false);
});

test("zero denominator should fail", () => {
expect(isProperFraction(100, 0)).toBe(false);
});

test("negative numerator should pass", () => {
expect(isProperFraction(-75, 100)).toBe(true);
});

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

test("Number card returns its numeric value", () => {
expect(getCardValue("2♥")).toEqual(2);
expect(getCardValue("7♦")).toEqual(7);
expect(getCardValue("10♣")).toEqual(10);
});

test("Face cards return 10", () => {
expect(getCardValue("J♣")).toEqual(10);
expect(getCardValue("Q♦")).toEqual(10);
expect(getCardValue("K♥")).toEqual(10);
});

test("Invalid rank throws an error", () => {
expect(() => getCardValue("11♠")).toThrow(Error);
expect(() => getCardValue("X♥")).toThrow(Error);
});

test("Invalid suit throws an error", () => {
expect(() => getCardValue("A★")).toThrow(Error);
});

test("Invalid format throws an error", () => {
expect(() => getCardValue("A")).toThrow(Error);
expect(() => getCardValue("♠A")).toThrow(Error);
expect(() => getCardValue(" A♠")).toThrow(Error);
expect(() => getCardValue("A♠♠")).toThrow(Error);
});

test("Arbitrary non-card string", () => {
expect(() => getCardValue("invalid")).toThrow();
});
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"keywords": [],
"author": "Code Your Future",
"license": "ISC",
"dependencies": {
"jest": "^29.7.0"
"devDependencies": {
"jest": "^30.5.2"
}
}
}
Loading