diff --git a/dead-code/exercise-1.js b/dead-code/exercise-1.js index 4d09f15fa9..5593f9e239 100644 --- a/dead-code/exercise-1.js +++ b/dead-code/exercise-1.js @@ -1,17 +1,6 @@ // Find the instances of unreachable and redundant code - remove them! // The sayHello function should continue to work for any reasonable input it's given. -let testName = "Jerry"; -const greeting = "hello"; - function sayHello(greeting, name) { - const greetingStr = greeting + ", " + name + "!"; return `${greeting}, ${name}!`; - console.log(greetingStr); } - -testName = "Aman"; - -const greetingMessage = sayHello(greeting, testName); - -console.log(greetingMessage); // 'hello, Aman!' diff --git a/dead-code/exercise-2.js b/dead-code/exercise-2.js index 56d7887c4c..16ca0657e9 100644 --- a/dead-code/exercise-2.js +++ b/dead-code/exercise-2.js @@ -2,27 +2,24 @@ // The countAndCapitalisePets function should continue to work for any reasonable input it's given, and you shouldn't modify the pets variable. const pets = ["parrot", "hamster", "horse", "dog", "hamster", "cat", "hamster"]; -const capitalisedPets = pets.map((pet) => pet.toUpperCase()); -const petsStartingWithH = pets.filter((pet) => pet[0] === "h"); - -function logPets(petsArr) { - petsArr.forEach((pet) => console.log(pet)); -} function countAndCapitalisePets(petsArr) { const petCount = {}; - petsArr.forEach((pet) => { - const capitalisedPet = pet.toUpperCase(); - if (petCount[capitalisedPet]) { - petCount[capitalisedPet] += 1; - } else { - petCount[capitalisedPet] = 1; - } - }); + petsArr + .filter((pet) => pet[0] === "h") + .forEach((pet) => { + const capitalisedPet = pet.toUpperCase(); + + if (petCount[capitalisedPet]) { + petCount[capitalisedPet] += 1; + } else { + petCount[capitalisedPet] = 1; + } + }); + return petCount; } -const countedPetsStartingWithH = countAndCapitalisePets(petsStartingWithH); - -console.log(countedPetsStartingWithH); // { 'HAMSTER': 3, 'HORSE': 1 } <- Final console log +const animals = ["lion", "hyena", "elephant", "hippo", "tiger", "hippo"]; +console.log(countAndCapitalisePets(animals)); diff --git a/format-clock-edge-cases/timeConverter.js b/format-clock-edge-cases/timeConverter.js index 310c07bd9c..192894d77e 100644 --- a/format-clock-edge-cases/timeConverter.js +++ b/format-clock-edge-cases/timeConverter.js @@ -1,11 +1,18 @@ function formatAs12HourClock(time) { - const hours = Number(time.slice(0, 2)); + const minutes = time.slice(3, 5); + + if (hours === 0) { + return `12:${minutes} am`; + } + if (hours === 12) { + return `12:${minutes} pm`; + } if (hours > 12) { - return `${hours - 12}:00 pm`; + return `${String(hours - 12).padStart(2, "0")}:${minutes} pm`; } return `${time} am`; } -export {formatAs12HourClock}; +export { formatAs12HourClock }; diff --git a/format-clock-edge-cases/timeConverter.test.js b/format-clock-edge-cases/timeConverter.test.js index 88f2af0948..8889b2c7b9 100644 --- a/format-clock-edge-cases/timeConverter.test.js +++ b/format-clock-edge-cases/timeConverter.test.js @@ -1,11 +1,66 @@ -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("can correctly convert time last minute of the day", function () { + assert.equal(formatAs12HourClock("23:59"), "11:59 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("can correctly convert time at midnight ", function () { + assert.equal(formatAs12HourClock("00:00"), "12:00 am"); +}); + +test("can correctly convert time just after midnight", function () { + assert.equal(formatAs12HourClock("00:01"), "12:01 am"); +}); + +test("can correctly convert time at the first normal hour", function () { + assert.equal(formatAs12HourClock("01:00"), "01:00 am"); +}); + +test("can correctly convert time on leading zeros in hour and minute", function () { + assert.equal(formatAs12HourClock("09:05"), "09:05 am"); +}); + +test("can correctly convert time just last minute before noon", function () { + assert.equal(formatAs12HourClock("11:59"), "11:59 am"); +}); + +test("can correctly convert time to PM at noon", function () { + assert.equal(formatAs12HourClock("12:00"), "12:00 pm"); +}); + +test("can correctly convert time just afternoon", function () { + assert.equal(formatAs12HourClock("12:01"), "12:01 pm"); +}); + +test("can correctly convert time last minute of the noon hour", function () { + assert.equal(formatAs12HourClock("12:59"), "12:59 pm"); +}); + +test("can correctly convert time first hour after noon", function () { + assert.equal(formatAs12HourClock("13:00"), "01:00 pm"); +}); + +test("correctly convert time before midnight", function () { + assert.equal(formatAs12HourClock("23:00"), "11:00 pm"); +}); + +/* +Edge cases to be tested. + Input Expected What it tests +1. 00:00 -> 12:00 am Midnight: hour 0 becomes 12 +2. 00:01 -> 12:01 am Just after midnight, still 12 +3. 01:00 -> 01:00 am First normal morning hour +4. 09:05 -> 09:05 am Leading zeros in hour and minute +5. 11:59 -> 11:59 am Last minute before noon +6. 12:00 -> 12:00 pm Noon: switches to pm, hour stays 12 +7. 12:01 -> 12:01 pm Just after noon, must not become 00 +8. 12:59 -> 12:59 pm Last minute of the noon hour +9. 13:00 -> 01:00 pm First hour after noon: 13 - 12, padded +10.23:59 -> 11:59 pm Last minute of the day +*/