diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index 310c07bd9..d941da6e8 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -1,11 +1,31 @@ -function formatAs12HourClock(time) { +// function formatAs12HourClock(time) { - const hours = Number(time.slice(0, 2)); +// const hours = Number(time.slice(0, 2)); - if (hours > 12) { - return `${hours - 12}:00 pm`; +// if (hours > 12) { +// return `${hours - 12}:00 pm`; +// } +// return `${time} am`; +// } + +// export {formatAs12HourClock}; + +function formatAs12HourClock(time){ + const [hourPart, minutePart] = time.split(":"); + const hours = Number(hourPart); + + let period; + if(hours < 12){ + period = "am"; + } else { + period = "pm"; } - return `${time} am`; -} + let displayHour = hours % 12; + if(displayHour === 0){ + displayHour = 12; + } + return `${displayHour}:${minutePart}${period}`; +} export {formatAs12HourClock}; +console.log(formatAs12HourClock("08:40")) diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index 88f2af094..ac64852b1 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -2,10 +2,40 @@ 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", () => { + assert.equal(formatAs12HourClock("23:00"), "11:00pm"); }); -test("can correctly convert morning time", function() { - assert.equal(formatAs12HourClock("08:00"), "08:00 am"); -}); +test("converts a single-digit morning hour and drops leading zero", () => { + assert.equal(formatAs12HourClock("08:00"), "8:00am"); +}) +test("converts exactly to midnight", () => { + assert.equal(formatAs12HourClock("00:00"), "12:00am"); +}) +test("converts time just after midnight", () => { + assert.equal(formatAs12HourClock("00:30"), "12:30am"); +}) +test("converts the last minute before noon", () => { + assert.equal(formatAs12HourClock("11:59"), "11:59am"); +}) +test("converts exact noon to pm", () => { + assert.equal(formatAs12HourClock("12:00"), "12:00pm"); +}) +test("convert time just after noon", () => { + assert.equal(formatAs12HourClock("12:30"), "12:30pm"); +}) +test("converts the first afternoon hour", () => { + assert.equal(formatAs12HourClock("13:00"), "1:00pm"); +}) +test("converts late evening hour", () => { + assert.equal(formatAs12HourClock("23:00"), "11:00pm"); +}) +test("converts last minute of the day", () => { + assert.equal(formatAs12HourClock("23:59"), "11:59pm"); +}) +test("preserves minutes with leading zero", () => { + assert.equal(formatAs12HourClock("09:05"), "9:05am"); +}) +test("preserves non-zero minutes", () => { + assert.equal(formatAs12HourClock("14:45"), "2:45pm"); +}) \ No newline at end of file diff --git a/implement-and-rewrite-tests/implement-tests-with-node-test/1-get-angle-type.test.js b/implement-and-rewrite-tests/implement-tests-with-node-test/1-get-angle-type.test.js index cf50f6cad..32d473387 100644 --- a/implement-and-rewrite-tests/implement-tests-with-node-test/1-get-angle-type.test.js +++ b/implement-and-rewrite-tests/implement-tests-with-node-test/1-get-angle-type.test.js @@ -10,3 +10,32 @@ test("Classifies right angles", () => { const right = getAngleType(90); assert.equal(right, "Right angle"); }); + +test("Classifies acute angles", () => { + assert.equal(getAngleType(45), "Acute angle"); + assert.equal(getAngleType(1), "Acute angle"); + assert.equal(getAngleType(89), "Acute angle"); +}); + +test("Classifies obtuse angles", () => { + assert.equal(getAngleType(91), "Obtuse angle"); + assert.equal(getAngleType(135), "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(181), "Reflex angle"); + assert.equal(getAngleType(270), "Reflex angle"); + assert.equal(getAngleType(359), "Reflex angle"); +}); + +test("Classifies invalid angles", () => { + assert.equal(getAngleType(0), "Invalid angle"); + assert.equal(getAngleType(360), "Invalid angle"); + assert.equal(getAngleType(400), "Invalid angle"); + assert.equal(getAngleType(-5), "Invalid angle"); +}) diff --git a/implement-and-rewrite-tests/implement-tests-with-node-test/2-is-proper-fraction.test.js b/implement-and-rewrite-tests/implement-tests-with-node-test/2-is-proper-fraction.test.js index a29084605..75ebbe7fd 100644 --- a/implement-and-rewrite-tests/implement-tests-with-node-test/2-is-proper-fraction.test.js +++ b/implement-and-rewrite-tests/implement-tests-with-node-test/2-is-proper-fraction.test.js @@ -7,6 +7,18 @@ import { isProperFraction } from "../implement/2-is-proper-fraction.js"; // What combinations of numerators and denominators should you test? test("Basic proper fraction", () => { - // Example: 1/2 is a proper fraction assert.equal(isProperFraction(1, 2), true); + assert.equal(isProperFraction(0, 5), true); + assert.equal(isProperFraction(-1, 2), true); + assert.equal(isProperFraction(1, -2), true); + assert.equal(isProperFraction(-1, -2), true); +}); + +test("Basic improper fraction", () => { + assert.equal(isProperFraction(5, 2), false); + assert.equal(isProperFraction(3, 3), false); + assert.equal(isProperFraction(5, 0), false); + assert.equal(isProperFraction(-5, 2), false); + assert.equal(isProperFraction(5, -2), false); + assert.equal(isProperFraction(-5, -2), false); }); diff --git a/implement-and-rewrite-tests/implement-tests-with-node-test/3-get-card-value.test.js b/implement-and-rewrite-tests/implement-tests-with-node-test/3-get-card-value.test.js index f5092d9b1..765ccd34d 100644 --- a/implement-and-rewrite-tests/implement-tests-with-node-test/3-get-card-value.test.js +++ b/implement-and-rewrite-tests/implement-tests-with-node-test/3-get-card-value.test.js @@ -9,8 +9,77 @@ test("Valid single-digit card", () => { assert.equal(getCardValue("9♠"), 9); }); +test("Ace returns 11", () => { + assert.equal(getCardValue("A♠"), 11); +}); + +test("Face cards return 10", () => { + assert.equal(getCardValue("J♣"), 10); + assert.equal(getCardValue("Q♦"), 10); + assert.equal(getCardValue("K♥"), 10); +}) + +test("Number cards 2 to 10 return their numbers", () => { + for(let n = 2; n <= 10; n++) { + assert.equal(getCardValue(`${n}♦`), n); + } +}); + +test("Only valid double-digit '10' card", () => { + assert.equal(getCardValue("10♥"), 10); +}); + +test("Accepts all four suits", () => { + for (const suit of ["♠", "♥", "♦", "♣"]){ + assert.equal(getCardValue(`5${suit}`), 5); + } +}); + test("Arbitrary non-card string", () => { assert.throws(() => getCardValue("invalid"), /Expected a number followed by a suit, but got "invalid"/, "Expected clear error"); }); // TODO: What other invalid card cases can you think of? +test("Missing suit", () => { + assert.throws(() => getCardValue("7"), /Expected a number followed by a suit/); + assert.throws(() => getCardValue("A"), /Expected a number followed by a suit/); +}); + +test("Missing ranks", () => { + assert.throws(() => getCardValue("♥"), /Expected a number followed by a suit/); +}); + +test("Empty strings", () => { + assert.throws(() => getCardValue(""), /Expected a number followed by a suit/); +}); + +test("Out-of-range number ranks", () => { + for (const OOR of ["0♥", "1♠", "11♦", "100♣"]) { + assert.throws(() => getCardValue(OOR), /Expected a number followed by a suit/); + } +}); + +test("Unknown suit", () => { + assert.throws(() => getCardValue("A❤"), /Expected a number followed by a suit/); + assert.throws(() => getCardValue("AV"), /Expected a number followed by a suit/); +}); + +test("Lowercase ranks", () => { + assert.throws(() => getCardValue("a♦"), /Expected a number followed by a suit/); + assert.throws(() => getCardValue("k♥"), /Expected a number followed by a suit/); +}); + +test("Suit before rank", () => { + assert.throws(() => getCardValue("♣A"), /Expected a number followed by a suit/); +}); + +test("Extra characters or spaces", () => { + for (const ExS of [" A♦", "A♦ ", "A♦♦", "A ♦"]){ + assert.throws(() => getCardValue(ExS), /Expected a number followed by a suit/); + } +}); + +test("Non-string input", () => { + assert.throws(() => getCardValue(undefined), /Expected a number followed by a suit/); + assert.throws(() => getCardValue(5), /Expected a number followed by a suit/); +}); \ No newline at end of file diff --git a/implement-and-rewrite-tests/implement/1-get-angle-type.js b/implement-and-rewrite-tests/implement/1-get-angle-type.js index 8a6fba050..4ff290d94 100644 --- a/implement-and-rewrite-tests/implement/1-get-angle-type.js +++ b/implement-and-rewrite-tests/implement/1-get-angle-type.js @@ -17,5 +17,17 @@ // execute the code to ensure all tests pass. export 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"; + } } diff --git a/implement-and-rewrite-tests/implement/2-is-proper-fraction.js b/implement-and-rewrite-tests/implement/2-is-proper-fraction.js index f983800f0..f56b51bf3 100644 --- a/implement-and-rewrite-tests/implement/2-is-proper-fraction.js +++ b/implement-and-rewrite-tests/implement/2-is-proper-fraction.js @@ -14,5 +14,9 @@ // execute the code to ensure all tests pass. export function isProperFraction(numerator, denominator) { - // TODO: Implement this function + if(Math.abs(numerator) < Math.abs(denominator)){ + return true; + } else { + return false; + } } diff --git a/implement-and-rewrite-tests/implement/3-get-card-value.js b/implement-and-rewrite-tests/implement/3-get-card-value.js index 9614ab1ee..05a47f6d2 100644 --- a/implement-and-rewrite-tests/implement/3-get-card-value.js +++ b/implement-and-rewrite-tests/implement/3-get-card-value.js @@ -24,5 +24,31 @@ // execute the code to ensure all tests pass. export function getCardValue(card) { - // TODO: Implement this function + const validSuits = ["♠", "♥", "♦", "♣"]; + const numberRanks = ["2", "3", "4", "5", "6", "7", "8", "9", "10"]; + const faceRanks = ["J", "Q", "K"]; + + const error = new Error(`Expected a number followed by a suit, but got "${card}"`); + + if(typeof card !== "string" || card.length < 2){ + throw error; + } + + const suit = card.slice(-1); + const rank = card.slice(0, -1); + + if(!validSuits.includes(suit)){ + throw error; + } + if(rank === "A") { + return 11; + } + if(faceRanks.includes(rank)){ + return 10; + } + if(numberRanks.includes(rank)){ + return Number(rank); + } + throw error; } +