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
1 change: 1 addition & 0 deletions implement-and-rewrite-tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
Original file line number Diff line number Diff line change
@@ -1,12 +1,90 @@
import assert from "node:assert";
import test from "node:test";
import test, { describe } 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");
const angleValue = getAngleType(90);
assert.equal(angleValue, "Right angle");
});

test("Classifies straight angles", () => {
const angleValue = getAngleType(180);
assert.equal(angleValue, "Straight angle");
});

describe ("Acute angles and boundaries", () => {
Comment thread
leigh-ross marked this conversation as resolved.
test("1 is acute", () => {
const angleValue = getAngleType(1);
assert.equal(angleValue, "Acute angle");
});

test("45 is acute", () => {
const angleValue = getAngleType(45);
assert.equal(angleValue, "Acute angle");
});

test("89 is acute", () => {
const angleValue = getAngleType(89);
assert.equal(angleValue, "Acute angle");
});
});

describe ("Obtuse angles and boundaries", () => {
test("91 is obtuse", () => {
const angleValue = getAngleType(91);
assert.equal(angleValue, "Obtuse angle");
});

test("135 is obtuse", () => {
const angleValue = getAngleType(135);
assert.equal(angleValue, "Obtuse angle");
});

test("179 is obtuse", () => {
const angleValue = getAngleType(179);
assert.equal(angleValue, "Obtuse angle");
});
});

describe ("Reflex angles and boundaries", () => {
test("181 is reflex", () => {
const angleValue = getAngleType(181);
assert.equal(angleValue, "Reflex angle");
});

test("270 is reflex", () => {
const angleValue = getAngleType(270);
assert.equal(angleValue, "Reflex angle");
});

test("359 is reflex", () => {
const angleValue = getAngleType(359);
assert.equal(angleValue, "Reflex angle");
});
});

describe ("Invalid angles and boundaries", () => {
test("0 is invalid", () => {
const angleValue = getAngleType(0);
assert.equal(angleValue, "Invalid angle");
});

test("5879 is invalid", () => {
const angleValue = getAngleType(5879);
assert.equal(angleValue, "Invalid angle");
});

test("-1 is invalid", () => {
const angleValue = getAngleType(-1);
assert.equal(angleValue, "Invalid angle");
});

test("360 is invalid", () => {
const angleValue = getAngleType(360);
assert.equal(angleValue, "Invalid angle");
});
});
Original file line number Diff line number Diff line change
@@ -1,12 +1,89 @@
import assert from "node:assert";
import test from "node:test";
import test, {describe} from "node:test";

import { isProperFraction } from "../implement/2-is-proper-fraction.js";

// TODO: Write tests to cover all cases.
// What combinations of numerators and denominators should you test?

test("Basic proper fraction", () => {
// Example: 1/2 is a proper fraction
describe ("Proper fractions", () => {
test("Basic proper fraction", () => {
assert.equal(isProperFraction(1, 2), true);
});

test("3/4 is proper", () => {
assert.equal(isProperFraction(3, 4), true);
});

test("2345678/98765234567 is proper", () => {
assert.equal(isProperFraction(2345678, 98765234567), true);
});

test("0/5 is proper", () => {
assert.equal(isProperFraction(0, 5), true);
});

test("99/100 is proper", () => {
assert.equal(isProperFraction(99, 100), true);
});

test("1/900 is proper", () => {
assert.equal(isProperFraction(1, 900), true);
});
});

describe ("Division by 0", () => {
test("1/0 is invalid", () => {
assert.equal(isProperFraction(1, 0), false);
});

test("19768/0 is invalid", () => {
assert.equal(isProperFraction(19768, 0), false);
});

test("0/0 is invalid", () => {
assert.equal(isProperFraction(0, 0), false);
});
});

describe ("Improper fractions", () => {
test("1/1 is improper", () => {
assert.equal(isProperFraction(1, 1), false);
});

test("19768/9 is improper", () => {
assert.equal(isProperFraction(19768, 9), false);
});

test("8/5 is improper", () => {
assert.equal(isProperFraction(8, 5), false);
});
});

describe ("Negative proper fractions", () => {
test("-98/100 is proper", () => {
assert.equal(isProperFraction(-98, 100), true);
});

test("-1/9 is proper", () => {
assert.equal(isProperFraction(-1, 9), true);
});

test("-8/15 is proper", () => {
assert.equal(isProperFraction(-8, 15), true);
});
});

describe("Negative improper fractions", () => {
test("-980/100", () => {
assert.equal(isProperFraction(-980, 100), false);
});

test("-19768/9 is improper", () => {
assert.equal(isProperFraction(-19768, 9), false);
});

test("-8/5 is improper", () => {
assert.equal(isProperFraction(-8, 5), false);
});
});
Original file line number Diff line number Diff line change
@@ -1,16 +1,69 @@
import assert from "node:assert";
import test from "node:test";
import test, {describe} from "node:test";

import { getCardValue } from "../implement/3-get-card-value.js";

// TODO: Write tests to cover all outcomes, including throwing errors for invalid cards.
describe ("Valid Face cards", () => {
test("Check Ace", () => {
assert.equal(getCardValue("A♠"), 11);
});
test("Check King", () => {
assert.equal(getCardValue("K♠"), 10);
})
test("Check Queen", () => {
assert.equal(getCardValue("Q♠"), 10);
})
test("Check Jack", () => {
assert.equal(getCardValue("J♠"), 10);
})
});

test("Valid single-digit card", () => {
assert.equal(getCardValue("9♠"), 9);
describe ("Valid number cards", () => {
test("Check 2", () => {
assert.equal(getCardValue("2♠"), 2);
});
test("Check 5", () => {
assert.equal(getCardValue("5♠"), 5);
})
test("Check 7", () => {
assert.equal(getCardValue("7♠"), 7);
})
test("Check 10", () => {
assert.equal(getCardValue("10♠"), 10);
});
});

test("Arbitrary non-card string", () => {
describe ("Invalid cards", () => {
test("Arbitrary non-card string", () => {
assert.throws(() => getCardValue("invalid"), /Expected a number followed by a suit, but got "invalid"/, "Expected clear error");
});
test("Out of bounds: 1♥", () => {
assert.throws(() => getCardValue("1♥"), /Expected a number followed by a suit, but got "1♥"/);
});
test("Out of bound: 11♥", () => {
assert.throws(() => getCardValue("11♥"), /Expected a number followed by a suit, but got "11♥"/);
});
});

// TODO: What other invalid card cases can you think of?
describe("Invalid card suits", () => {
test("Arbitrary non-card string", () => {
assert.throws(
() => getCardValue("2O"),
/Expected card suit to be: "♠", "♥", "♦", "♣" but got "2O"/,
"Expected clear error"
);
});
test("Out of bounds: 1d", () => {
assert.throws(
() => getCardValue("8d"),
/Expected card suit to be: "♠", "♥", "♦", "♣" but got "8d"/
);
});
test("Out of bound: 11X", () => {
assert.throws(
() => getCardValue("9X"),
//`Expected card suit to be: "♠", "♥", "♦", "♣" but got ${cardSuit}`
/Expected card suit to be: "♠", "♥", "♦", "♣" but got "9X"/
);
});
});
18 changes: 16 additions & 2 deletions implement-and-rewrite-tests/implement/1-get-angle-type.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Implement a function getAngleType
//
// Don't forget to write tests in implement-tests-with-node-test.
//
// When given an angle in degrees, it should return a string indicating the type of angle:
Expand All @@ -17,5 +16,20 @@
// execute the code to ensure all tests pass.

export function getAngleType(angle) {
// TODO: Implement this function
if (angle === 90) {
return "Right angle"
}
else if (angle === 180) {
return "Straight angle"
}
else if (angle > 0 && angle < 90) {
return "Acute angle"
}
else if (angle > 90 && angle < 180) {
return "Obtuse angle"
}
else if (angle > 180 && angle < 360) {
return "Reflex angle"
}
return "Invalid angle"
}
4 changes: 2 additions & 2 deletions implement-and-rewrite-tests/implement/2-is-proper-fraction.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Implement a function isProperFraction,
// Implement a function isProperFraction, fix
//
// Don't forget to write tests in implement-tests-with-node-test.
//
Expand All @@ -14,5 +14,5 @@
// execute the code to ensure all tests pass.

export function isProperFraction(numerator, denominator) {
// TODO: Implement this function
return Math.abs(numerator) < Math.abs(denominator)
}
63 changes: 38 additions & 25 deletions implement-and-rewrite-tests/implement/3-get-card-value.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
// This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck
/*
This problem involves playing cards: https://en.wikipedia.org/wiki/Standard_52-card_deck

// Don't forget to write tests in implement-tests-with-node-test.

// Implement a function getCardValue, when given a string representing a playing card,
// should return the numerical value of the card.

// A valid card string will contain a rank followed by the suit.
// The rank can be one of the following strings:
// "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"
// The suit can be one of the following emojis:
// "♠", "♥", "♦", "♣"
// For example: "A♠", "2♥", "10♥", "J♣", "Q♦", "K♦".

// When the card is an ace ("A"), the function should return 11.
// When the card is a face card ("J", "Q", "K"), the function should return 10.
// When the card is a number card ("2" to "10"), the function should return its numeric value.

// When the card string is invalid (not following the above format), the function should
// throw an error.

// Acceptance criteria:
// After you have implemented the function, write tests to cover all the cases, and
// execute the code to ensure all tests pass.
Implement a function getCardValue, when given a string representing a playing card,
should return the numerical value of the card.
A valid card string will contain a rank followed by the suit.
The rank can be one of the following strings:
"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"
The suit can be one of the following emojis:
"♠", "♥", "♦", "♣"
For example: "A♠", "2♥", "10♥", "J♣", "Q♦", "K♦".
When the card is an ace ("A"), the function should return 11.
When the card is a face card ("J", "Q", "K"), the function should return 10.
When the card is a number card ("2" to "10"), the function should return its numeric value.
When the card string is invalid (not following the above format), the function should
throw an error.
Acceptance criteria:
After you have implemented the function, write tests to cover all the cases, and
execute the code to ensure all tests pass.
*/

export function getCardValue(card) {
// TODO: Implement this function
}
const cardValue = card.slice(0,-1);
const cardSuit = card.slice(-1);
const validSuits = ["♠", "♥", "♦", "♣"];
const validNumberCards = ["2", "3", "4", "5", "6", "7", "8", "9", "10"]
const validFaceCards = ["A", "K", "Q", "J"];
const cardValueNumber = Number(cardValue)
if (!validNumberCards.includes(cardValue) && !validFaceCards.includes(cardValue)){
throw new Error(`Expected a number followed by a suit, but got "${card}"`);
} else if (!validSuits.includes(cardSuit)) {
throw new Error(`Expected card suit to be: "♠", "♥", "♦", "♣" but got "${card}"`);
}
if (cardValue === "A") {
return 11;
} else if (validFaceCards.includes(cardValue)) {
return 10;
} else {
return cardValueNumber;
}
}
Loading
Loading