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
19 changes: 12 additions & 7 deletions format-clock-edge-cases/timeConverter.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
function formatAs12HourClock(time) {

export function formatAs12HourClock(time) {
const hours = Number(time.slice(0, 2));
const minutes = time.slice(2);

const period = hours >= 12 ? "pm" : "am";

let hours12 = hours % 12;

if (hours > 12) {
return `${hours - 12}:00 pm`;
if (hours12 === 0) {
hours12 = 12;
}
return `${time} am`;
}

export {formatAs12HourClock};
const formattedHours = String(hours12).padStart(2, "0");

return `${formattedHours}${minutes} ${period}`;
}
66 changes: 61 additions & 5 deletions format-clock-edge-cases/timeConverter.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,67 @@
import {formatAs12HourClock} from "./timeConverter.js";
import { formatAs12HourClock } from "./timeConverter.js";
import assert from "node:assert";
import test from "node:test";

test("correctly convert time after 12:00", function(){
assert.equal(formatAs12HourClock("23:00"), "11:00 pm");
test("correctly convert time after 12:00", function () {
assert.equal(formatAs12HourClock("23:00"), "11:00 pm");
});

test("can correctly convert morning time", function() {
assert.equal(formatAs12HourClock("08:00"), "08:00 am");
test("can correctly convert morning time", function () {
assert.equal(formatAs12HourClock("08:00"), "08:00 am");
});

test("converts midnight correctly", function () {
assert.equal(formatAs12HourClock("00:00"), "12:00 am");
});

test("converts one minute after midnight", function () {
assert.equal(formatAs12HourClock("00:01"), "12:01 am");
});

test("converts early morning time", function () {
assert.equal(formatAs12HourClock("01:30"), "01:30 am");
});

test("converts last minute before noon", function () {
assert.equal(formatAs12HourClock("11:59"), "11:59 am");
});

test("converts noon correctly", function () {
assert.equal(formatAs12HourClock("12:00"), "12:00 pm");
});

test("converts one minute after noon", function () {
assert.equal(formatAs12HourClock("12:01"), "12:01 pm");
});

test("converts afternoon time", function () {
assert.equal(formatAs12HourClock("13:30"), "01:30 pm");
});

test("converts evening time", function () {
assert.equal(formatAs12HourClock("18:45"), "06:45 pm");
});

test("converts last minute of the day", function () {
assert.equal(formatAs12HourClock("23:59"), "11:59 pm");
});

test("converts 09:59 correctly", function () {
assert.equal(formatAs12HourClock("09:59"), "09:59 am");
});

test("converts 10:00 correctly", function () {
assert.equal(formatAs12HourClock("10:00"), "10:00 am");
});

test("converts 12:59 correctly", function () {
assert.equal(formatAs12HourClock("12:59"), "12:59 pm");
});

test("converts 13:00 correctly", function () {
assert.equal(formatAs12HourClock("13:00"), "01:00 pm");
});

test("converts 23:00 correctly", function () {
assert.equal(formatAs12HourClock("23:00"), "11:00 pm");
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,35 @@ 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 acute angles", () => {
assert.equal(getAngleType(45), "Acute angle");
assert.equal(getAngleType(1), "Acute angle");
assert.equal(getAngleType(89), "Acute angle");
});

test("Classifies right angles", () => {
const right = getAngleType(90);
assert.equal(right, "Right angle");
assert.equal(getAngleType(90), "Right angle");
});

test("Classifies obtuse angles", () => {
assert.equal(getAngleType(120), "Obtuse angle");
assert.equal(getAngleType(91), "Obtuse angle");
assert.equal(getAngleType(179), "Obtuse angle");
});

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

test("Classifies reflex angles", () => {
assert.equal(getAngleType(270), "Reflex angle");
assert.equal(getAngleType(181), "Reflex angle");
assert.equal(getAngleType(359), "Reflex angle");
});

test("Identifies invalid angles", () => {
assert.equal(getAngleType(0), "Invalid angle");
assert.equal(getAngleType(360), "Invalid angle");
assert.equal(getAngleType(-1), "Invalid angle");
assert.equal(getAngleType(361), "Invalid angle");
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,31 @@ import test 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
test("Identifies basic proper fractions", () => {
assert.equal(isProperFraction(1, 2), true);
assert.equal(isProperFraction(3, 4), true);
});

test("Identifies improper fractions", () => {
assert.equal(isProperFraction(5, 3), false);
assert.equal(isProperFraction(4, 4), false);
});

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

test("Handles negative numerators", () => {
assert.equal(isProperFraction(-1, 2), true);
assert.equal(isProperFraction(-5, 2), false);
});

test("Handles negative denominators", () => {
assert.equal(isProperFraction(1, -2), true);
assert.equal(isProperFraction(3, -2), false);
});

test("Handles zero denominator", () => {
assert.equal(isProperFraction(1, 0), false);
assert.equal(isProperFraction(0, 0), false);
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,35 @@ import test 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.
test("Returns the correct value for number cards", () => {
assert.equal(getCardValue("2♠"), 2);
assert.equal(getCardValue("5♥"), 5);
assert.equal(getCardValue("9♦"), 9);
assert.equal(getCardValue("10♣"), 10);
});

test("Returns 11 for aces", () => {
assert.equal(getCardValue("A♠"), 11);
});

test("Valid single-digit card", () => {
assert.equal(getCardValue("9♠"), 9);
test("Returns 10 for face cards", () => {
assert.equal(getCardValue("J♠"), 10);
assert.equal(getCardValue("Q♥"), 10);
assert.equal(getCardValue("K♦"), 10);
});

test("Arbitrary non-card string", () => {
assert.throws(() => getCardValue("invalid"), /Expected a number followed by a suit, but got "invalid"/, "Expected clear error");
test("Accepts all four suits", () => {
assert.equal(getCardValue("5♠"), 5);
assert.equal(getCardValue("5♥"), 5);
assert.equal(getCardValue("5♦"), 5);
assert.equal(getCardValue("5♣"), 5);
});

// TODO: What other invalid card cases can you think of?
test("Rejects invalid cards", () => {
assert.throws(() => getCardValue("invalid"));
assert.throws(() => getCardValue("1♠"));
assert.throws(() => getCardValue("11♠"));
assert.throws(() => getCardValue("5X"));
assert.throws(() => getCardValue("♠5"));
assert.throws(() => getCardValue("5"));
});
22 changes: 21 additions & 1 deletion implement-and-rewrite-tests/implement/1-get-angle-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,25 @@
// execute the code to ensure all tests pass.

export function getAngleType(angle) {
// TODO: Implement this function
if (angle <= 0 || angle >= 360) {
return "Invalid angle";
}

if (angle < 90) {
return "Acute angle";
}

if (angle === 90) {
return "Right angle";
}

if (angle < 180) {
return "Obtuse angle";
}

if (angle === 180) {
return "Straight angle";
}

return "Reflex angle";
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
// execute the code to ensure all tests pass.

export function isProperFraction(numerator, denominator) {
// TODO: Implement this function
return denominator !== 0 && Math.abs(numerator) < Math.abs(denominator);
}
18 changes: 17 additions & 1 deletion 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 validCard = /^(A|[2-9]|10|J|Q|K)[♠♥♦♣]$/;

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

const rank = card.slice(0, -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,34 @@
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
test("Classifies acute angles", () => {
expect(getAngleType(1)).toEqual("Acute angle");
expect(getAngleType(45)).toEqual("Acute angle");
expect(getAngleType(89)).toEqual("Acute angle");
});

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

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

test("Classifies straight angles", () => {
expect(getAngleType(180)).toEqual("Straight angle");
});

test("Classifies reflex angles", () => {
expect(getAngleType(181)).toEqual("Reflex angle");
expect(getAngleType(270)).toEqual("Reflex angle");
expect(getAngleType(359)).toEqual("Reflex angle");
});

test("Identifies invalid angles", () => {
expect(getAngleType(0)).toEqual("Invalid angle");
expect(getAngleType(360)).toEqual("Invalid angle");
expect(getAngleType(-1)).toEqual("Invalid angle");
expect(getAngleType(361)).toEqual("Invalid angle");
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
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.
test("Identifies positive proper fractions", () => {
expect(isProperFraction(1, 2)).toEqual(true);
expect(isProperFraction(3, 4)).toEqual(true);
});

test("Identifies improper fractions", () => {
expect(isProperFraction(5, 3)).toEqual(false);
expect(isProperFraction(4, 4)).toEqual(false);
});

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

test("Handles negative numerators", () => {
expect(isProperFraction(-1, 2)).toEqual(true);
expect(isProperFraction(-5, 2)).toEqual(false);
});

test("Handles negative denominators", () => {
expect(isProperFraction(1, -2)).toEqual(true);
expect(isProperFraction(3, -2)).toEqual(false);
});

// Special case: numerator is zero
test(`should return false when denominator is zero`, () => {
test("Handles zero denominator", () => {
expect(isProperFraction(1, 0)).toEqual(false);
expect(isProperFraction(0, 0)).toEqual(false);
});
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
import { getCardValue } from "../implement/3-get-card-value.js";

// TODO: Write tests in Jest syntax to cover all possible outcomes.

// Case 1: Ace (A)
test(`Should return 11 when given an ace card`, () => {
test("Returns 11 for aces", () => {
expect(getCardValue("A♠")).toEqual(11);
expect(getCardValue("A♥")).toEqual(11);
expect(getCardValue("A♦")).toEqual(11);
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
test("Returns the correct value for number cards", () => {
expect(getCardValue("2♠")).toEqual(2);
expect(getCardValue("5♥")).toEqual(5);
expect(getCardValue("9♦")).toEqual(9);
expect(getCardValue("10♣")).toEqual(10);
});

// To learn how to test whether a function throws an error as expected in Jest,
// please refer to the Jest documentation:
// https://jestjs.io/docs/expect#tothrowerror
test("Returns 10 for face cards", () => {
expect(getCardValue("J♠")).toEqual(10);
expect(getCardValue("Q♥")).toEqual(10);
expect(getCardValue("K♦")).toEqual(10);
});

test("Throws an error for invalid cards", () => {
expect(() => getCardValue("invalid")).toThrow();
expect(() => getCardValue("1♠")).toThrow();
expect(() => getCardValue("11♠")).toThrow();
expect(() => getCardValue("5X")).toThrow();
expect(() => getCardValue("♠5")).toThrow();
expect(() => getCardValue("5")).toThrow();
});
Loading