Repository navigation
London | 26-ITP-SEPT | Carol Nassuna | Sprint 1 | Exhaustively test and fix formatAs12HourClock #1657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
London | 26-ITP-SEPT | Carol Nassuna | Sprint 1 | Exhaustively test and fix formatAs12HourClock #1657
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,22 @@ | ||
| function formatAs12HourClock(time) { | ||
|
|
||
| const hours = Number(time.slice(0, 2)); | ||
| const minutes = time.slice(-2); | ||
|
|
||
| if (hours > 12) { | ||
| return `${hours - 12}:00 pm`; | ||
| if (time == "00") { | ||
| return `12:00:${minutes} am`; | ||
| } else if (hours > 12) { | ||
| return `${hours - 12}:${minutes} pm`; | ||
| } else if (hours === 12) { | ||
| return `12:${minutes} pm`; | ||
| } else { | ||
| return `${time} am`; | ||
| } | ||
| return `${time} am`; | ||
| } | ||
|
|
||
| export {formatAs12HourClock}; | ||
| console.log(formatAs12HourClock("23:00")); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These five lines were useful while you were working it out, but they print every time the tests run: you can see |
||
| console.log(formatAs12HourClock("14:00")); | ||
| console.log(formatAs12HourClock("12:00")); | ||
| console.log(formatAs12HourClock("08:15")); | ||
| console.log(formatAs12HourClock("00:00")); | ||
|
|
||
| export { formatAs12HourClock }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,39 @@ | ||
| 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("can correctly convert midday", function () { | ||
| assert.equal(formatAs12HourClock("12:00"), "12:00 pm"); | ||
| }); | ||
|
|
||
| test("can correctly convert midnight", function () { | ||
| assert.equal(formatAs12HourClock("00:00"), "12:00 am"); | ||
| }); | ||
|
|
||
| test("can correctly convert 2 hours before 12:00", function () { | ||
| assert.equal(formatAs12HourClock("10:00"), "10:00 am"); | ||
| }); | ||
|
|
||
| test("can correctly convert 30 minutes before 12:00", function () { | ||
| assert.equal(formatAs12HourClock("11:30"), "11:30 am"); | ||
| }); | ||
|
|
||
| test("can correctly convert time at the beginning of noon", function () { | ||
| assert.equal(formatAs12HourClock("12:001"), "12:01 pm"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's an extra |
||
| }); | ||
|
|
||
| test("can correctly convert half hour before midnight", function () { | ||
| assert.equal(formatAs12HourClock("23:30"), "11:30 pm"); | ||
| }); | ||
|
|
||
| test("can correctly convert 1 minute before midnight", function () { | ||
| assert.equal(formatAs12HourClock("23:59"), "11:59 pm"); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is why the midnight test fails.
Line 5 checks whether
timeis"00". But for midnight,timeis the whole text"00:00", so the check is never true, and the code carries on down to line 12, which returns"00:00 am".Line 2 already gives you the hour on its own, as a number, in the variable
hours. Change line 5 so it checkshoursinstead oftime. Rememberhoursis a number, so compare it with a number, not with text in quotes.Then look at line 6. It returns
12:00:followed by the minutes, which gives12:00:00 am, one part too many. Line 10 is how you did it for noon. Midnight needs the same shape, but withaminstead ofpm.Run
node --test timeConverter.test.jsin the terminal again. The midnight test should now show ✔.