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
11 changes: 0 additions & 11 deletions dead-code/exercise-1.js
Original file line number Diff line number Diff line change
@@ -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!'
31 changes: 14 additions & 17 deletions dead-code/exercise-2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
13 changes: 10 additions & 3 deletions format-clock-edge-cases/timeConverter.js
Original file line number Diff line number Diff line change
@@ -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 };
65 changes: 60 additions & 5 deletions format-clock-edge-cases/timeConverter.test.js
Original file line number Diff line number Diff line change
@@ -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
*/
Loading