-
Notifications
You must be signed in to change notification settings - Fork 57
feat: add solution for day 08 #273
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?
Conversation
WalkthroughA new JavaScript function Changes
Sequence DiagramsequenceDiagram
participant F as tinhNgayDenTet Function
participant D as Date Object
participant C as Console
F->>D: Get current time
F->>D: Get Tết Nguyên Đán time
loop Every Second
F->>D: Calculate time difference
alt Time remaining > 0
F->>C: Log remaining time
else Time reached
F->>C: Log Tết arrival message
F-->>F: Stop interval
end
end
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
|
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.
Actionable comments posted: 4
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro
📒 Files selected for processing (1)
daily-challenges/2024-10/08/hunghoang3110.js
(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: guardrails/scan
// Javascript | ||
function tinhNgayDenTet() { | ||
const giay = 1000; | ||
const phut = giay * 60; | ||
const gio = phut * 60; | ||
const ngay = gio * 24; | ||
const ngayTet = new Date("01/29/2025"); | ||
const interval = setInterval(() => { | ||
let thoiGianConLai; | ||
const ngayHienTai = new Date(); | ||
thoiGianConLai = ngayTet.getTime() - ngayHienTai.getTime(); | ||
if (thoiGianConLai <= 0) { | ||
clearInterval(interval); | ||
console.log('Tết Nguyên Đán 2025'); | ||
} else { | ||
console.log(`Chỉ còn ${Math.floor(thoiGianConLai / ngay)} ngày,` | ||
+ ` ${Math.floor(thoiGianConLai % ngay / gio)} giờ, ${Math.floor(thoiGianConLai % gio / phut)} phút,` | ||
+ ` ${Math.floor(thoiGianConLai % phut / giay)} giây nữa là đến Tết Nguyên Đán 2025` | ||
) | ||
} | ||
}, 1000); | ||
} |
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.
🧹 Nitpick (assertive)
Consider separating concerns for better maintainability
The current implementation mixes time calculation, display formatting, and interval handling. Consider splitting this into separate modules:
- A pure calculation function for time difference
- A formatting module for display strings
- A controller for managing the interval updates
This separation would make the code more testable and maintainable.
if (thoiGianConLai <= 0) { | ||
clearInterval(interval); | ||
console.log('Tết Nguyên Đán 2025'); | ||
} else { | ||
console.log(`Chỉ còn ${Math.floor(thoiGianConLai / ngay)} ngày,` | ||
+ ` ${Math.floor(thoiGianConLai % ngay / gio)} giờ, ${Math.floor(thoiGianConLai % gio / phut)} phút,` | ||
+ ` ${Math.floor(thoiGianConLai % phut / giay)} giây nữa là đến Tết Nguyên Đán 2025` | ||
) | ||
} | ||
}, 1000); |
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.
🧹 Nitpick (assertive)
Improve readability and flexibility
- Extract time calculations into separate functions
- Add proper handling for negative intervals
- Consider making the output method configurable
Here's a suggested improvement:
+ const formatTimeUnit = (remaining, unit) => Math.floor(remaining % unit);
+ const formatOutput = (days, hours, minutes, seconds) =>
+ `Chỉ còn ${days} ngày, ${hours} giờ, ${minutes} phút, ${seconds} giây nữa là đến Tết Nguyên Đán 2025`;
+
const interval = setInterval(() => {
const thoiGianConLai = target.getTime() - ngayHienTai.getTime();
if (thoiGianConLai <= 0) {
clearInterval(interval);
- console.log('Tết Nguyên Đán 2025');
+ onComplete?.('Tết Nguyên Đán 2025');
} else {
- console.log(`Chỉ còn ${Math.floor(thoiGianConLai / ngay)} ngày,`
- + ` ${Math.floor(thoiGianConLai % ngay / gio)} giờ, ${Math.floor(thoiGianConLai % gio / phut)} phút,`
- + ` ${Math.floor(thoiGianConLai % phut / giay)} giây nữa là đến Tết Nguyên Đán 2025`
- )
+ const days = Math.floor(thoiGianConLai / DAY);
+ const hours = formatTimeUnit(thoiGianConLai % DAY, HOUR);
+ const minutes = formatTimeUnit(thoiGianConLai % HOUR, MINUTE);
+ const seconds = formatTimeUnit(thoiGianConLai % MINUTE, MILLISECOND);
+ onUpdate?.(formatOutput(days, hours, minutes, seconds));
}
}, 1000);
Committable suggestion skipped: line range outside the PR's diff.
Summary by CodeRabbit